How can I extract all words, which are common in a list of strings?
Example:
//Output = Bammler GOV
"Bammler Tokyo SA GOV"
"Zurich Bammler GOV"
"London Bammler 12 GOV"
"New Bammler York GOV"
I tried following:
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);
}
I found this here Find a common string within a list of strings but this will just extract for example "Bammler".