Possible Duplicate:
Are there any Fuzzy Search or String Similarity Functions libraries written for C#?
I am creating an application which will except user input of a Song or Artist or Album name and then will look through a String Array or ArrayList for any possible matches.
The auto suggestions will be calculated based on the match percentage.
For example
If user types link prk it should find Linkin Park
or Link 80
or Link Wray
but the match percentage will be different for all
Assume that the collection will only search for Artist names in Artist Collection and Song name in song collection.
(Percentage figures are just for explanation)
Linkin Park - 98%
Link Wray -82%
Link 80 - 62%
Solution does not have to be C# code, any regex or pseudo code will be good but should be implementable in C#.
Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.
Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.
C strcmp() The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.
In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.
You're looking for Levenshtein distance
Here is an implementation in C#.
Here is a Generic Implementation of the Levenshtein Distance. (as in the Diff/Dist. between two IEnum<T>
's)
Implementations of Levenshtein Distance Algorithm in a LOT of languages.
Usually an implementation of the Levenshtein distance also called edit distance is used for this. This will find matches based on the minimum number of edits needed to transform one string into the other, counting all insertions, deletions, or substitutions of a single character as a measure for the "cost" - candidates are then strings that have the minimum cost.
Here's a link to an article with a generic implementation in C#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With