h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Compare string in C#.net


In this Article, we discuss about a string Compare and split a strings.
t is a sample code for Compare string.
 In My project, I used 4 controls.The controls are Two Textbox ,One Label and One Button.
  Here, I create a simple function for Comparing two string using C#.net .
  Input :
      1st TextBox: My name is Lakshmi Naraayanan
      2nd TextBox: My name is Naraayanan   
Output:
     Lakshmi.

Code:
  
private string  CompareText(string fValue,string svalue)
 {
            string formHeading = fValue;
            string userSearch = svalue ;
            string constring = string.Empty;
            string comstring = string.Empty;
            string[] formHeadingWords;
            string[] userSearchWords;
 formHeadingWords = System.Text.RegularExpressions.Regex.Split(formHeading, @"\W");
 userSearchWords = System.Text.RegularExpressions.Regex.Split(userSearch, @"\W");
            int cntFarray = formHeadingWords.Length;
            int cntSarray = userSearchWords.Length;
            if (cntSarray > cntFarray)
            {
                foreach (string _str in userSearchWords)
                {
                    if (!formHeading.Contains(_str))
                    {
                       comstring = _str;
                    }
                    if (comstring.Trim() != constring.Trim())
                    {
                        constring = constring + " " + comstring;
                    }
                }
            }
            else
            {
                foreach (string _str in formHeadingWords)
                {
                    if (!userSearch.Contains(_str))
                    {
                        comstring = _str;
                    }
                    if (comstring.Trim() != constring.Trim())
                    {
                       constring = constring + " " + comstring;
                    }
                }
            }
           return constring;
         }
call this button click event:
  string str = CompareText(textBox1.Text,textBox2.Text );
            label1.Text = str;
            label1.ForeColor = Color.Red;

No comments:

Post a Comment