从字符串中搜索子字符串并获取最匹配的子字符串!字符串比较,返回最相似
string [] allModels = { "Galaxy", "S3", "Galaxy S3" };
string title = "Samasung galaxy s3 is for sale";
string[] title_array = title.Split(' ');
string model = "";
foreach(var tit in title_array)
{
foreach(var mod in allModels)
{
if (mod.Equals(tit, StringComparison.OrdinalIgnoreCase))
{
model = mod;
}
}
}
选择的模式是Galaxy
但我需要Galaxy S3
(即,最相似)。我怎样才能得到Galaxy S3
。
我应该用Array.FindAll(target)
的方法吗?
更新:
通过most similar
我指的是子串(模型)相匹配的最从字符串(标题)
例如,在galaxy Samasung s3 is for sale
的模式应该是galaxy s3
(根据上述allModels
阵列)
'allModels'不包含'Length'的定义。错误 –
@IrfanWattoo正确复制代码,我没有使用*长度*作为'allModels'顺便说一下:在发布之前,我在代码上面运行:) –
对不起,我的错误,如果标题像'galaxy Samasung s3 is for sale' 。在这种情况下代码将不起作用 –