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;
|
You must Sign In To post reply |