[英]Compare list of strings and string to find number of consecutive character matches
Im sure this has been done before, but what Im really trying to do is have a method that finds consecutive matches of characters for one string, within a set of multiple strings, with any matches less than 1 character match, not counting.我确定这之前已经完成了,但我真正想做的是有一种方法可以在一组多个字符串中找到一个字符串的连续字符匹配,其中任何匹配小于 1 个字符匹配,不计算在内。 (minimum of 2). (最少 2 个)。
if the string to test with was "xx Audible 5", the following would be the result如果要测试的字符串是“xx Audible 5”,则结果如下
Try this.尝试这个。 This is not optimized but it works pretty well这没有优化,但效果很好
static int FindMatch(string text, string pattern)
{
var total = 0;
for (int i = 0; i < pattern.Length; i++)
{
var max = 0;
for (int j = 2; j <= pattern.Length - i; j++)
{
var temp = pattern.Substring(i, j);
if (text.Contains(temp))
if (max < temp.Length)
max = temp.Length;
}
total += max;
if (max > 0)
i += max-1;
}
return total;
}
FindMatch("Audible xxx-xxx-5051 NJ", "xx Audible 5");
returns 10返回 10
FindMatch("yy Audible", "xx Audible 5");
returns 8返回 8
FindMatch("Audible 5 xy", "xx Audible 5");
returns 9返回 9FindMatch("Audible.com 5", "xx Audible 5");
returns 9 too and not 7 because as I understood in this example would fail space 5 " 5"也返回 9 而不是 7 因为正如我在这个例子中所理解的那样会失败 space 5 " 5"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.