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

回眸只為那壹抹淺笑 提交于 2019-12-18 15:52:42

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5444945/how-to-check-if-two-string-are-a-partial-match-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!