Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two string are a partial match in C#? [duplicate]

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#.

like image 360
Kamil Dhuleshia Avatar asked Mar 26 '11 20:03

Kamil Dhuleshia


People also ask

How do you match a partial string?

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.

How do you compare two substrings?

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.

How do you check if a string is equal to another string in C?

C strcmp() The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.

Can we compare two strings in C?

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.


2 Answers

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.

like image 33
gideon Avatar answered Sep 24 '22 22:09

gideon


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#.

like image 72
BrokenGlass Avatar answered Sep 23 '22 22:09

BrokenGlass