Darkloki Darkloki - 1 year ago 243
C# Question

Replace Case Insensitive StringBuilder

I have read many times over about the virtue of using Stringbuilder vs. string when multiple replacements are needed. I need to convert an HTML string (all very simple formatting) to WIKI notation and was going to roll my own from code I found on SO. I'm wondering if there is a simpler way to do this, and also how I can implement the benefits of a StringBuilder with it. All my attempts til now have failed miserably. Please advise and thank you in advance.

public static string ConvertHTML2WIKInotation(string htmlstring)
StringBuilder sb = new StringBuilder(htmlstring);

htmlstring = ReplaceInsensitive(htmlstring, "<hr>", "----");
htmlstring = ReplaceInsensitive(htmlstring, "<hr />", "----");
... etc.

return sb.ToString();

static public string ReplaceInsensitive(this string str, string from, string to)
str = Regex.Replace(str, from, to, RegexOptions.IgnoreCase);
return str;

Answer Source

Since html tags case insensitive, I would suggest non StringBuilder since SB is not recommended for the scenarios where you need more replace string.

public static string ReplaceMultiple(
          string template, 
          IEnumerable<KeyValuePair<string, string>> replaceParameters)
   var result = template;

   foreach(var replace in replaceParameters)
      var templateSplit = Regex.Split(result, 
      result = string.Join(replace.Value, templateSplit);

   return result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download