如何提取所有在字符串列表中常見的單詞?c#查找列表中的所有常見子字符串<string>
例子:
//Output = Bammler GOV
"Bammler Tokyo SA GOV"
"Zurich Bammler GOV"
"London Bammler 12 GOV"
"New Bammler York GOV"
我嘗試以下操作:
static void Main(string[] args)
{
List<string> MyStringList = new List<string>()
{
"Bammler Tokyo SA GOV",
"Zurich Bammler GOV",
"London Bammler 12 GOV",
"New Bammler York GOV"
};
string shortest = MyStringList.OrderBy(s => s.Length).First();
IEnumerable<string> shortestSubstrings = getAllSubstrings(shortest).OrderByDescending(s => s.Length);
var other = MyStringList.Where(s => s != shortest).ToArray();
string longestCommonIntersection = string.Empty;
foreach (string subStr in shortestSubstrings)
{
bool allContains = other.All(s => s.Contains(subStr));
if (allContains)
{
longestCommonIntersection = subStr;
break;
}
}
}
public static IEnumerable<string> getAllSubstrings(string word)
{
return from charIndex1 in Enumerable.Range(0, word.Length)
from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
where charIndex2 >= 2
select word.Substring(charIndex1, charIndex2);
}
我發現這個在這裏Find a common string within a list of strings但這將只是提取例如 「Bammler」。
你嘗試過什麼?你不能指望我們爲你做所有的工作...... – maccettura
StackOverflow以這種方式工作 - 你**嘗試**自己解決任務,如果你有一些問題(錯誤或意外的結果),那麼你給**問題描述**,你目前的**代碼**,並要求人們幫助你。 –