C# 中如何检查两个字符串是否部分匹配?
我正在创建一个应用程序,它将排除用户输入的歌曲或艺术家或专辑名称,然后将查找字符串数组或 ArrayList 是否有任何可能的匹配项。
自动建议将根据匹配百分比计算。
例如
,如果用户输入link prk,它应该找到Linkin Park
或Link 80
或Link Wray
,但匹配百分比假设
集合将仅搜索艺术家集合中的艺术家姓名和歌曲集合中的歌曲名称。
(百分比数字仅用于说明)
Linkin Park - 98%
Link Wray -82%
Link 80 - 62%
解决方案不必是 C# 代码,任何正则表达式或伪代码都可以,但应该可以在 C# 中实现。
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#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,使用 Levenshtein 距离(也称为编辑距离)的实现来实现此目的。这将根据将一个字符串转换为另一个字符串所需的最小编辑次数来查找匹配项,计算单个字符的所有插入、删除或替换作为“成本”的衡量标准 - 候选字符串就是具有最小成本的字符串。
以下是一篇文章的链接,其中包含通用内容在 C# 中的实现。
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#.
您正在寻找编辑距离
这是 C# 中的实现。
这是 编辑距离的通用实现。 (如两个
IEnum
之间的 Diff/Dist.)Levenshtein 距离算法在多种语言中的实现。
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.