title: c# Find all common substring in List<string>
body: <p>How can I extract all words, which are common in a list of strings?</p>
  
  <p>Example:</p>
  
  <pre><code>//Output = Bammler GOV
    "Bammler Tokyo SA GOV"
    "Zurich Bammler GOV"
    "London Bammler 12 GOV"
    "New Bammler York GOV"
  </code></pre>
  
  <p>I tried following:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          List&lt;string&gt; MyStringList = new List&lt;string&gt;()
          {
              "Bammler Tokyo SA GOV",
              "Zurich Bammler GOV",
              "London Bammler 12 GOV",
              "New Bammler York GOV"
          };
  
          string shortest = MyStringList.OrderBy(s =&gt; s.Length).First();
          IEnumerable&lt;string&gt; shortestSubstrings = getAllSubstrings(shortest).OrderByDescending(s =&gt; s.Length);
          var other = MyStringList.Where(s =&gt; s != shortest).ToArray();
          string longestCommonIntersection = string.Empty;
          foreach (string subStr in shortestSubstrings)
          {
              bool allContains = other.All(s =&gt; s.Contains(subStr));
              if (allContains)
              {
                  longestCommonIntersection = subStr;
                  break;
              }
          }
      }
  
      public static IEnumerable&lt;string&gt; getAllSubstrings(string word)
      {
          return from charIndex1 in Enumerable.Range(0, word.Length)
                 from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
                 where charIndex2 &gt;= 2
                 select word.Substring(charIndex1, charIndex2);
      }
  </code></pre>
  
  <p>I found this here <a href="https://stackoverflow.com/questions/13509277/find-a-common-string-within-a-list-of-strings">Find a common string within a list of strings</a> but this will just extract for example "Bammler".</p>