Peter Petrov’s Weblog

var me = from practice in programming where practice.IsBestPractice && practice.UseLambda select practice.OptimalPerformance;

Useful method – 7 of N – Ignore case on String.Replace() June 27, 2008

Filed under: .NET Framework,C# — ppetrov @ 3:51 pm
Tags: , , ,

I’ve posted a method to determine if a string contains another string ignoring the case. I’ve also looked all string methods and I’ve found Replace isn’t available in a ignore case variant. I’ve created the missing overload(using an extension methods).

public static string Replace(this string original, string oldValue, string newValue, StringComparison comparisionType)
 if (oldValue == null)
 throw new ArgumentNullException("oldValue");
 if (newValue == null)
 throw new ArgumentNullException("newValue");

 var result = original;

 if (oldValue != newValue)
 int index = -1;
 int lastIndex = 0;

 var buffer = new StringBuilder();

 while ((index = original.IndexOf(oldValue, index + 1, comparisionType)) >= 0)
 buffer.Append(original, lastIndex, index - lastIndex);

 lastIndex = index + oldValue.Length;
 buffer.Append(original, lastIndex, original.Length - lastIndex);

 result = buffer.ToString();
 return result;

UPDATE : I’ve updated the method to allow newValue to be string.Empty. It will perform like a remove method. WordPress source code posting is broken 😦


6 Responses to “Useful method – 7 of N – Ignore case on String.Replace()”

  1. Sergey Says:

    Thanks, it’s just what I needed. And your blog is full of valuable information. Greets from Kazakhstan!

  2. Marcos Says:

    Excelent Method 🙂

    Just a little optimization in the line when u define the String Builder u can use

    StringBuilder buffer = new StringBuilder(original.Length);

    To avoid internal resize when the string is too big

    Best Regards

    • ppetrov Says:

      I would say: “To allocate unnecessarily when the string is small” 🙂

      I like to specify the capacity of the buffer only when I know what will be the size of the resulting string. In this case I don’t know it ( it depends on the result of IndexOf ). If the result is 10(100) times smaller then the original it will be a waste to allocate so much memory.
      I see your point but I will leave it like it is – without specifying the capacity of the buffer.

  3. The Bit Says:

    As another option you could just use:
    string result = Regex.Replace(“hello world”, “world”, “john”, RegexOptions.IgnoreCase);

  4. ppetrov Says:

    Yes, I like regexes. The only thing that I will do is to escape the pattern to avoid regex “collision”. Something like this :
    string result = Regex.Replace(“hello world”, Regex.Escape(“world”), “john”, RegexOptions.IgnoreCase);

  5. Diego Says:

    You should change the conditional to:

    if (oldValue != newValue || (comparisionType != StringComparison.Ordinal))

    “AAAAA”.replace(“a”, “a”, StringComparison.OrdinalIgnoreCase) should return “aaaaa”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s