locked
Compare two string and highlight RRS feed

  • Question

  • User1910487977 posted

    https://www.aspforums.net/Threads/198450/Compare-two-strings-and-highlight-changes-using-C-and-VBNet-in-ASPNet/

    The below is code which is working fine. When both has same type of data.

    Eg: Textbox1 = John Wright

    Textbox2 = John Wrright

    The above condition is working fine.

    But when Textbox1 = John Wright and Textbox2 = John then it shows error. Like 

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    What is the solution to avoid this error.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TEst2.aspx.cs" Inherits="TEst2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    
        <asp:Label ID="LblMessage" runat="server" ></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Validate" onclick="Button1_Click" />
    
       
        </form>
    </body>
    </html>
    
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    using System.Drawing;
    using System.IO;
    using System.Data;
    using System.Net.Mail;
    
    using System.Web.Services;
    using System.Web.Script.Services;
    public partial class TEst2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            LblMessage.Text = "";
    
            string message1 = TextBox1.Text;
            string message2 = TextBox2.Text;
    
            StringCollection stringCollectionmessage1 = new StringCollection();
            StringCollection stringCollectionmessage2 = new StringCollection();
    
            stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
            stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
            for (int i = 0; i < stringCollectionmessage1.Count; i++)
            {
                if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                {
                    LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                }
                else
                {
                    LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                }
            }
        }
    }

    Tuesday, November 5, 2019 9:09 AM

Answers

  • User-1038772411 posted

    Hello

    As we checked and added a condition for the checking the count of both StringCollection.

    You can try using below code.

    private void button1_Click(object sender, EventArgs e)
            {
                LblMessage.Text = "";
    
                string message1 = TextBox1.Text;
                string message2 = TextBox2.Text;
    
                StringCollection stringCollectionmessage1 = new StringCollection();
                StringCollection stringCollectionmessage2 = new StringCollection();
    
                stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
                stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
                for (int i = 0; i < stringCollectionmessage1.Count; i++)
                {
                    if(!(stringCollectionmessage1.Count > stringCollectionmessage2.Count)) //Condition Added
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else
                    {
                        LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                    }
                }
            }

    If you still facing issue then let us know.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 5, 2019 10:33 AM
  • User288213138 posted

    Hi UmerFaiz001,

    Eg: Textbox1 = John Wright

    Textbox2 = John Wrright

    The above condition is working fine.

    But when Textbox1 = John Wright and Textbox2 = John then it shows error. Like 

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    What is the solution to avoid this error.

    You should make different judgment conditions according to different situations.

    The code:

     protected void Button1_Click(object sender, EventArgs e)
            {
                LblMessage.Text = "";
    
                string message1 = TextBox1.Text;
                string message2 = TextBox2.Text;
    
                StringCollection stringCollectionmessage1 = new StringCollection();
                StringCollection stringCollectionmessage2 = new StringCollection();
    
                stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
                stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
                
                int j = stringCollectionmessage1.Count > stringCollectionmessage2.Count ? stringCollectionmessage1.Count : stringCollectionmessage2.Count;
                for (int i = 0; i < j; i++)
                {
                    if (Convert.ToInt32(stringCollectionmessage1.Count) == Convert.ToInt32(stringCollectionmessage2.Count))
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else if (Convert.ToInt32(stringCollectionmessage1.Count) > Convert.ToInt32(stringCollectionmessage2.Count))
                    {
                        if (!stringCollectionmessage2.Contains(stringCollectionmessage1[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage1[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage2[i].ToString() + " ";
                        }
                    }
    
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 3:20 AM

All replies

  • User-1038772411 posted

    Hello

    As we checked and added a condition for the checking the count of both StringCollection.

    You can try using below code.

    private void button1_Click(object sender, EventArgs e)
            {
                LblMessage.Text = "";
    
                string message1 = TextBox1.Text;
                string message2 = TextBox2.Text;
    
                StringCollection stringCollectionmessage1 = new StringCollection();
                StringCollection stringCollectionmessage2 = new StringCollection();
    
                stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
                stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
                for (int i = 0; i < stringCollectionmessage1.Count; i++)
                {
                    if(!(stringCollectionmessage1.Count > stringCollectionmessage2.Count)) //Condition Added
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else
                    {
                        LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                    }
                }
            }

    If you still facing issue then let us know.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 5, 2019 10:33 AM
  • User-1780421697 posted

    I think that its more about validation of data type and length, two strings are equal if they have equal length ,You need to check the length of both text array and then you can compare.

    May be you can compare string word by word as well, break the sentence in the form of words and then you can have some business logic to compare after performing some sort of validation.

    Tuesday, November 5, 2019 10:33 AM
  • User1910487977 posted

    Hello

    As we checked and added a condition for the checking the count of both StringCollection.

    You can try using below code.

    private void button1_Click(object sender, EventArgs e)
            {
                LblMessage.Text = "";
    
                string message1 = TextBox1.Text;
                string message2 = TextBox2.Text;
    
                StringCollection stringCollectionmessage1 = new StringCollection();
                StringCollection stringCollectionmessage2 = new StringCollection();
    
                stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
                stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
                for (int i = 0; i < stringCollectionmessage1.Count; i++)
                {
                    if(!(stringCollectionmessage1.Count > stringCollectionmessage2.Count)) //Condition Added
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else
                    {
                        LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                    }
                }
            }

    If you still facing issue then let us know.

    Thanks.

    The above work is working fine but the problem is

    when textbox1.text = "John Wright" and textbox2 = "John" then it is not highlighting the Wright as difference

    Tuesday, November 5, 2019 10:42 AM
  • User-1780421697 posted

    I think you need to divide sentence in words and then compare words, when word match then highlight it. Debug the code it will give you exact issue.

    Tuesday, November 5, 2019 11:49 AM
  • User-1038772411 posted

    Hello

    We have added another code for the split using the space(' ') and then after compare.

    The code is mentioned below.

                var collection1 = TextBox1.Text.ToLower().Split(' ');
                var collection2 = TextBox2.Text.ToLower().Split(' ');
                foreach (var item in collection1)
                {
                    if (collection2.Contains(item))
                    {
                        LblMessage.Text += item + " ";
                    }
                    else
                    {
                        LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + item + "</font>" + " ";
                    }
                }

    If you still facing issue then let us know.

    Thanks.

    Tuesday, November 5, 2019 12:29 PM
  • User1910487977 posted

    Hello

    We have added another code for the split using the space(' ') and then after compare.

    The code is mentioned below.

                var collection1 = TextBox1.Text.ToLower().Split(' ');
                var collection2 = TextBox2.Text.ToLower().Split(' ');
                foreach (var item in collection1)
                {
                    if (collection2.Contains(item))
                    {
                        LblMessage.Text += item + " ";
                    }
                    else
                    {
                        LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + item + "</font>" + " ";
                    }
                }

    If you still facing issue then let us know.

    Thanks.

    Error

    'System.Array' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

    Wednesday, November 6, 2019 3:00 AM
  • User288213138 posted

    Hi UmerFaiz001,

    Eg: Textbox1 = John Wright

    Textbox2 = John Wrright

    The above condition is working fine.

    But when Textbox1 = John Wright and Textbox2 = John then it shows error. Like 

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    What is the solution to avoid this error.

    You should make different judgment conditions according to different situations.

    The code:

     protected void Button1_Click(object sender, EventArgs e)
            {
                LblMessage.Text = "";
    
                string message1 = TextBox1.Text;
                string message2 = TextBox2.Text;
    
                StringCollection stringCollectionmessage1 = new StringCollection();
                StringCollection stringCollectionmessage2 = new StringCollection();
    
                stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
                stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    
                
                int j = stringCollectionmessage1.Count > stringCollectionmessage2.Count ? stringCollectionmessage1.Count : stringCollectionmessage2.Count;
                for (int i = 0; i < j; i++)
                {
                    if (Convert.ToInt32(stringCollectionmessage1.Count) == Convert.ToInt32(stringCollectionmessage2.Count))
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else if (Convert.ToInt32(stringCollectionmessage1.Count) > Convert.ToInt32(stringCollectionmessage2.Count))
                    {
                        if (!stringCollectionmessage2.Contains(stringCollectionmessage1[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage1[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
                        }
                    }
                    else
                    {
                        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
                        {
                            LblMessage.Text += "<font color='Red' style='font-weight: 700; font-style: italic; text-decoration: underline'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
                        }
                        else
                        {
                            LblMessage.Text += stringCollectionmessage2[i].ToString() + " ";
                        }
                    }
    
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 6, 2019 3:20 AM