Find all possible combinations of word with and without hyphens
<p>For a string that may have zero or more hyphens in it, I need to extract all the different possibilities with and without hyphens.</p>

<p>For example, the string "A-B" would result in "A-B" and "AB" (two possibilities).</p>

<p>The string "A-B-C" would result in "A-B-C", "AB-C", "A-BC" and "ABC" (four possibilities).</p>

<p>The string "A-B-C-D" would result in "A-B-C-D", "AB-C-D", "A-BC-D", "A-B-CD", "AB-CD", "ABC-D", "A-BCD" and "ABCD" (eight possibilities).</p>

<p>...etc, etc.</p>

<p>I've experimented with some nested loops but haven't been able to get anywhere near the desired result. I suspect I need something recursive unless there is some simple solution I am overlooking.</p>

<p>NB. This is to build a SQL query (shame that SQL Server does't have MySQL's REGEXP pattern matching).</p>

<p>Here is one attempt I was working on. This might work if I do this recursively.</p>

<pre><code>string keyword = "A-B-C-D";

List&lt;int&gt; hyphens = new List&lt;int&gt;();

int pos = keyword.IndexOf('-');
while (pos != -1)
{
    hyphens.Add(pos);
    pos = keyword.IndexOf('-', pos + 1);
}

for (int i = 0; i &lt; hyphens.Count(); i++)
{
    string result = keyword.Substring(0, hyphens[i]) + keyword.Substring(hyphens[i] + 1);

    Response.Write("&lt;p&gt;" + result);
}
</code></pre>

<p>A B C D are words of varying length.</p>