title: All Common Substrings Between Two Strings
body: <p>I am working on C# to find all the common substrings between two strings. For instance, if the input is: 
  S1= "need asssitance with email"
  S2= "email assistance needed"</p>
  
  <p>The output should be- 'need assistance email' </p>
  
  <p>The below code returns the longest common substring, but I want my code to return all the common substrings.  Any help is much appreciated! </p>
  
  <pre><code>static void commonsubstrings()
      {
          input1 = "need asssitance with email";
          input2 = "email assistance needed"
  
          if (input2.Length &gt; input1.Length)
          {
              swap = input1;
              input1 = input2;
              input2 = swap;
          }
  
          int k = 1;
          String temp;
          String longTemp = "";
          for (int i = 0; (i &lt;= input1.Length); i++)
          {
              if ((i == input1.Length))
              {
                  if (longest != null)
                  {
                      k = longest.Length + 1;
                  }
                  else
                  {
                      k = 1;
                  }
                  temp = input1.Substring(1, input1.Length - 1);
                  if (temp.Equals(""))
                  {
                      break;
                  }
                  if (k &lt;= temp.Length)
                  {
                      i = k - 1;
                      input1 = temp;
                      if ((longest != null) &amp;&amp; (longest.Length &gt; longTemp.Length))
                      {
                          longTemp = longest;
                      }
                  }
              }
              holder1 = input1.Substring(0, k);
  
              for (int j = 0; (j &lt; input2.Length) &amp;&amp; (j + k &lt;= input2.Length); j++)
              {
                  check1 = input2.Substring(j, k);
                  if (holder1.Equals(check1))
                  {
                      longest = holder1;
                      break;
                  }
              }
              k++;
          }
  
          Console.WriteLine(longest);
          Console.ReadLine(); 
  </code></pre>
  
  <p>}</p>