Click here to Skip to main content
12,698,012 members (25,587 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VB 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 20:53pm
Updated 3-Oct-12 21:00pm
JF201555K
v2
Comments
   
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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2017
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