Touchy Vivace
Touchy Vivace

Reputation: 304

Get a diff from between two string

I have a two strings and need to get just a diff between them like this

dim st1 As String = "SELECT C1,C2,C3,C4,C5 FROM TEST"
dim st2 As String = "SELECT C1,C2,C3 FROM TEST"

I need to compare and show that "st2 does not have ,C4,C5"

so I try to use

Dim differenceQuery = st1.Except(st2)
lbldiff.Text = "st2 not have "
For Each name As String In differenceQuery
    lbldiff.Text += name & Environment.NewLine
Next

but this only shows 4 5

Upvotes: 1

Views: 65

Answers (2)

Touchy Vivace
Touchy Vivace

Reputation: 304

I found my solution to solved my problem

   Dim diff As List(Of String)
        Dim set1 As IEnumerable(Of String) = sql.ToLower.Split(","c).Distinct()
        Dim set2 As IEnumerable(Of String) = answer.ToLower.Split(","c).Distinct()
        If set2.Count() > set1.Count() Then
            diff = set2.Except(set1).ToList()
        Else
            diff = set1.Except(set2).ToList()
        End If
        If diff IsNot Nothing AndAlso diff.Count <> 0 Then
            lbldiff.Text = "you have a problem on : <br />"
            For Each li As String In diff
                lbldiff.Text += li & " <br /> "
            Next

Upvotes: 0

Furtiro
Furtiro

Reputation: 449

I wrote this for you, test and edit it if you need. This is C# but you can translate it to vb i think ( not the best way to achieve this and not optimize at all )

        private List<string> CompareSQLString(string t1,string t2,string table)
    {

        List<string> res = new List<string>();

        t1 = t1.Replace("SELECT", "").Replace("FROM", "").Replace(table,"").Replace("  ", string.Empty);
        t2 = t2.Replace("SELECT", "").Replace("FROM", "").Replace(table, "").Replace("  ", string.Empty);

        List<string> res1 = new List<string>(t1.Split(','));
        List<string> res2 = new List<string>(t2.Split(','));


            foreach(string val in res1)
            {
                if(!res2.Contains(val))
                {
                    res.Add(val);
                }
            }


        return res;
    }

You can use it like this :

        string t1 = "SELECT C1,C2,C3,C4,C5 FROM TEST";
        string t2 = "SELECT C1,C2,C3 FROM TEST";
        List<string> test = CompareSQLString(t1, t2,"TEST");

test will contains two values, C4 and C5

EDIT : Don't hesitate to ask comments or explanations

Upvotes: 1

Related Questions