title: How to do a multiple case insensitive replace using a StringBuilder
body: <p>I have a (large) template and want to replace multiple values. The replacement needs to be done case insensitive. It must also be possible to have keys that does not exist in the template.</p>
  
  <p>For example:</p>
  
  <pre><code>[TestMethod]
  public void ReplaceMultipleWithIgnoreCaseText()
  {
      const string template = "My name is @Name@ and I like to read about @SUBJECT@ on @website@, tag  @subject@";  
      const string expected = "My name is Alex and I like to read about C# on stackoverflow.com, tag C#";
      var replaceParameters = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
      {
          new KeyValuePair&lt;string, string&gt;("@name@","Alex"),
          new KeyValuePair&lt;string, string&gt;("@subject@","C#"),
          new KeyValuePair&lt;string, string&gt;("@website@","stackoverflow.com"),
          // Note: The next key does not exist in template 
          new KeyValuePair&lt;string, string&gt;("@country@","The Netherlands"), 
      };
      var actual = ReplaceMultiple(template, replaceParameters);
      Assert.AreEqual(expected, actual);
  }
  
  public string ReplaceMultiple(
                    string template, 
                    IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt; replaceParameters)
  {
      throw new NotImplementedException(
                    "Implementation needed for many parameters and long text.");
  }
  </code></pre>
  
  <p>Note that if I have a list of 30 parameters and a large template, I do not want 30 large strings in memory. Using a StringBuilder seems to be an option, but other solutions are also welcome.</p>
  
  <p><strong>Solution I tried but did not work</strong></p>
  
  <p>Solution found here (<a href="https://stackoverflow.com/questions/1231768/c-sharp-string-replace-with-dictionary">C# String replace with dictionary</a>) throws an exception when a key is not in the colletion, but our users makes mistakes and in that case I want to just leave the wromg key in the text. Example:</p>
  
  <pre><code>static readonly Regex re = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
  static void Main2()
  {
      // "Name" is accidentally typed by a user as "nam". 
      string input = @"Dear $nam$, as of $date$ your balance is $amount$"; 
  
      var args = new Dictionary&lt;string, string&gt;(
          StringComparer.OrdinalIgnoreCase) {
      {"name", "Mr Smith"},
      {"date", "05 Aug 2009"},
      {"amount", "GBP200"}};
  
  
      // Works, but not case insensitive and 
      // uses a lot of memory when using a large template
      // ReplaceWithDictionary many args
      string output1 = input;
      foreach (var arg in args)
      {
          output1 = output1.Replace("$" + arg.Key +"$", arg.Value);
      }
  
      // Throws a KeyNotFoundException + Only works when data is tokenized
      string output2 = re.Replace(input, match =&gt; args[match.Groups[1].Value]);
  }
  </code></pre>