Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
Hi,
 
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
JF201553.2K
v2
Comments
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?
--SA
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
good
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))
                {
                    unMatch++;
                }
                Place++;
            }
 
            MessageBox.Show(unMatch + " unmatched character(s) found.");
            }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:
C#:
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])
                {
                    unequalPositions.Add(i);
                }
            }
 
VB.NET:
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
        unequalPositions.Add(i)
    End If
Next
 
Now, you'll find the zero-based unequal positions in the List.
  Permalink  

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 563
1 George Jonsson 258
2 OriginalGriff 250
3 CPallini 210
4 PIEBALDconsult 145
0 OriginalGriff 5,865
1 Sergey Alexandrovich Kryukov 5,316
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
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