How to "grow" a string to a wanted length
<p>I am trying to "grow" a string to a desired length (i.e. "ABCDE", desired length 7, after padding becomes "--ABCDE", "-A-BCDE", "-AB-CDE", ......, "ABCDE--").</p>

<p>When I was trying to pad "ABCDEFGH" to length 16 using following code, the code runs forever.</p>

<pre><code>/// &lt;summary&gt;
    /// Pad a str to desired length
    /// &lt;/summary&gt;
    /// &lt;param name="s"&gt;&lt;/param&gt;
    /// &lt;param name="length"&gt;&lt;/param&gt;
    /// &lt;param name="pad"&gt;&lt;/param&gt;
    /// &lt;param name="Padded"&gt;&lt;/param&gt;
    public static void PadToLength(string s, int length, char pad, ref List&lt;string&gt; Padded)
    {
        if (s.Length == length)
        {
            Padded.Add(s);
            return;
        }
        else if (s.Length &gt; length)
        {
            return;
        }
        else
        {
            List&lt;int&gt; pos = GetExceptPos(s, pad.ToString());
            pos.Sort();

            int count = -1;
            foreach (int p in pos)
            {
                count++;

                // Pad left 
                string leftPadStr = s.Substring(0, p) + pad + s.Substring(p);
                PadToLength(leftPadStr, length, pad, ref Padded);

                // Pad right at the last pos
                if (count == pos.Count - 1)
                {
                    string rightPadStr = s + pad;
                    PadToLength(rightPadStr, length, pad, ref Padded);
                }
            }
        }
    }

        /// &lt;summary&gt;
    /// Find indexes for elements different from target str
    /// &lt;/summary&gt;
    /// &lt;param name="str"&gt;&lt;/param&gt;
    /// &lt;param name="excludeStr"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    private static List&lt;int&gt; GetExceptPos(string str, string excludeStr)
    {
        List&lt;int&gt; allIndexes = new List&lt;int&gt;();
        for (int i = 0; i &lt; str.Length; i++)
        {
            allIndexes.Add(i);
        }

        return allIndexes.Except(str.IndexesOf(excludeStr)).ToList();
    }
</code></pre>

<p>Any suggestion will be appreciated. Thanks.</p>
