This article illustrates the different techniques to calculate the similarity between two strings in C#.

We can use the Edit distance algorithm to determine the similarity between two strings in C#. The Edit distance algorithm tells us how different two strings are from each other by finding the least number of moves (add, remove, insert) required to convert one string to another. We can even use the Jaro-Winkler distance algorithm instead of Edit distance.

 
The following code provides the C# implementation of the Edit distance algorithm and uses it to find the similarity between two given strings:

Download  Run Code

 
The above code calculates the similarity between two strings in the closed range [0, 1]. We can easily modify the return value of the findSimilarity() routine to calculate similarity in percentage (return value x 100).

That’s all about calculating the similarity between two strings in C#.