How to compare 2 variables letter by letter


I need to compare 2 last names. If both last names are the same length but if 1 or 2 letters are different at the same characters positions to account for obvious misspellings, it would be considered a match. For example - Smith = Smitn
How can I check each character position? Thanks


You can check if length of both strings and string values are same. If the length is same and there is difference in value, convert string to character array and get the difference text. Please find below code snippet.

String text1 = “Smitn”, text2=“Smith”, result;
if (text1.Length == text2.Length && String.Compare(text1, text2) != 0)
result = String.Join(String.Empty,text1.ToCharArray().Except(text2.ToCharArray()));
Console.WriteLine("Strings are " + (result.Length > 2 ? “Different”: “Same”));

You can write everything in one assign statement.
If you want to ignore case as well, convert both strings to lower case before manipulation.
Hope this helps!


Thanks for your help!