.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Compare string in C#.net

Posted By:Narayanan       Posted Date: February 03, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

In this Article, we discuss about a string Compare and split a strings.
 

 It 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;

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend