I want to compare two text strings(nvarchar characters) & want to find their comparison result. Example:
string 1: We loved our country.it is beautiful and amazing too.
string 2: We loved our nice country.it is beautifully amazing too.
Expected result:
No. of change: 3
they are: nice,beautifully,and.
i have tried:
private string TextComparison(string textFirst,string textSecond)
{
Difference difference = new Difference();
return result = difference.DifferenceMain(textFirst, textSecond);
}
I need a function DifferenceMain()
.
Try this
string st1 = "We loved our country.it is beautiful and amazing too";
string st2 = "We loved our nice country.it is beautifully amazing too";
List<string> diff;
List<string> diff1;
IEnumerable<string> str1 = st1.Split(' ').Distinct();
IEnumerable<string> str2 = st2.Split(' ').Distinct();
diff = str2.Except(str1).ToList();
diff1 = str1.Except(str2).ToList();
diff will give you following result
nice
beautifully
_ (Space) - as your 2nd string contains extra space
diff1 will give you following result
beautiful
and
Use this function.
public string[] GetChanges(string one, string two)
{
string[] wordsonone = one.Split(" ".ToCharArray());//Creates a string array by splitting the string into individual words.
string[] wordsontwo = two.Split(" ".ToCharArray());
List<string> changes = new List<string>();//Create a string list to contain all the changes.
for(int i = 0; i < wordsonone.Length; i++)
{
if(!wordsontwo.Contains(wordsonone[i]))//If wordsontwo doesn't contain this word.
{
changes.Add(wordsonone[i]);//Add this word to changes
}
}
for (int i = 0; i < wordsontwo.Length; i++)
{
if(!wordsonone.Contains(wordsontwo[i]))
{
changes.Add(wordsontwo[i]);
}
}
return changes.ToArray();
}
And in order to present it in your format, you would use the following.
string[] changes = GetChanges(string1,string2);
string text = "Numbers of Changes "+changes.Length+", these changes are ";
foreach(string change in changes)
{
text += change+", ";
}
MessageBox.Show(text);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.