Click here to Skip to main content
11,721,053 members (82,741 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# VB.NET

I have 2 textboxes where the user enters texts of same length. I need to do a char by char comparison. For this, i used Chars(i). Now, i have been trying to highlight the differences at the string index which is different. I am not able to figure a way out for that.

for example:

string1 = HELLO
string2 = WELLM

When the user hits Compare button, the code should highlight H and W, and also O and M maybe in the same respective textboxes or 2 different labels etc. Could you suggest ideas as to how i can do that? I am pretty much a newbie with VB coding! Have been trying to do this for a long time.
Posted 3-Oct-12 19:53pm
Edited 3-Oct-12 20:00pm
Sergey Alexandrovich Kryukov at 4-Oct-12 1:57am
If you are a newbie, you need more effort, not less, but what you show here is nearly zero effort. Going to be experienced, ever?
VijayChauhan123 at 4-Oct-12 2:10am
You have to generate dynamic labels based on the highest length of the textbox values, then fill the text property of the labels with the chars from the textbox and also set the forecolor of the label based on the index of the unmatched char.
DaveAuld at 4-Oct-12 4:31am
One option is to use a loop and also character index positions, go figure.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try modifying this to what you want...

First, add reference to the namespace Microsoft.VisualBasic
this, allows Strings.Mid to be available.

using Microsoft.VisualBasic;
string First = "Hello";
            string Test = "Wello";
            int unMatch = 0;
            int Place = 1;
            foreach (char current in First)
                if (Place == Test.Length) break;
                if (current.ToString() != Strings.Mid(Test, Place, 1))
            MessageBox.Show(unMatch + " unmatched character(s) found.");
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:
List<int> unequalPositions = new List<int>();
            string str1 = "HELLO";
            string str2 = "WELLM";
            for (int i = 0; i < str1.Length; i++)
                if (str1[i] != str2[i])

Dim unequalPositions As New List(Of Integer)()
Dim str1 As String = "HELLO"
Dim str2 As String = "WELLM"
For i As Integer = 0 To str1.Length - 1
    If str1(i) <> str2(i) Then
    End If

Now, you'll find the zero-based unequal positions in the List.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 500
1 OriginalGriff 334
2 Mika Wendelius 160
3 F-ES Sitecore 140
4 Maciej Los 80
0 Sergey Alexandrovich Kryukov 2,199
1 OriginalGriff 959
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100