![](/img/trans.png)
[英]How to compare 2 string arrays and find all consecutive matches and save indices?
[英]Compare list of strings and string to find number of consecutive character matches
我确定这之前已经完成了,但我真正想做的是有一种方法可以在一组多个字符串中找到一个字符串的连续字符匹配,其中任何匹配小于 1 个字符匹配,不计算在内。 (最少 2 个)。
如果要测试的字符串是“xx Audible 5”,则结果如下
尝试这个。 这没有优化,但效果很好
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");
返回 10
FindMatch("yy Audible", "xx Audible 5");
返回 8
FindMatch("Audible 5 xy", "xx Audible 5");
返回 9FindMatch("Audible.com 5", "xx Audible 5");
也返回 9 而不是 7 因为正如我在这个例子中所理解的那样会失败 space 5 " 5"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.