The C# string library is pretty comprehensive, but there are a few methods that were left out. Luckily, in C# 3.0 extensions were introduced, which allows developers to seemingly add methods to existing classes. Remember that extensions only work inside the same namespace, so you’ll have to rename “MyNamespace” in the example below.
Two of the missing methods are case-insensitive versions of the methods “Contains” and “Replace”. I do not take credit for writing the methods — I found them on Google and have provided references in the code. I did add the comments to make the overloaded methods match the originals. I also tested them to make sure they behaved like the originals when provided with a null or empty string.
I’m not sure why the .NET team did not provide case-insensitive Replace or case-insensitive Contains, they do it with many other string methods like StartsWith and EndsWith. Hopefully these help.
The case-insensitive Contains code is heavily based on a post on this forum. The case-insensitive Replace code is heavily based on a Code Project comment. Learn more about C# extensions on MSDN.
Please note that I had to butcher some of the XML comments because the code formatter was deleting them. It shouldn’t affect compiling, but you may have to fix some if you want the full Intellisense.
using System;
using System.Text;
namespace MyNamespace
{
/// <summary>
/// Overloads the string object and adds case-insenstive options to certain methods
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Returns a value indicating whether the specified System.String object occurs
/// within this string.
/// </summary>
/// <param name="original">The System.String object to be checked.</param>
/// <param name="value">The System.String object to seek.</param>
/// <param name="comparisonType">One of the System.StringComparison values that
/// determines how this string and value are compared.</param>
/// <returns>true if the value parameter occurs within this string, or
/// if value is the empty string (""); otherwise, false.</returns>
/// <exception cref="System.ArgumentNullException">value is null.</exception>
static public bool Contains(this string original, string value, StringComparison comparisonType)
{
if (original == null)
throw new NullReferenceException("Object reference not set to an instance of an object.");
if (value == null)
throw new ArgumentNullException("value");
if (String.Empty == value)
return true;
return (0 < = original.IndexOf(value, comparisonType));
}
/// <summary>
/// Replaces all occurrences of a specified System.String in this instance
/// with another specified System.String. < / summary>
/// <param name="original">The System.String object to be searched.</param>
/// <param name="oldValue">A System.String to be replaced.</param>
/// <param name="newValue">A System.String to replace all occurrences of oldValue.</param>
/// <param name="comparisonType">One of the System.StringComparison values that
/// determines how this string and value are compared.</param>
/// <returns>A System.String equivalent to this instance but with all
/// instances of oldValue replaced with newValue.</returns>
/// <exception cref="System.ArgumentNullException">oldValue is null.</exception>
/// <exception cref="System.ArgumentException">oldValue is the empty string ("").</exception>
static public string Replace(this string original, string oldValue, string newValue, StringComparison comparisonType)
{
if (original == null)
throw new NullReferenceException("Object reference not set to an instance of an object.");
if (oldValue == null)
throw new ArgumentNullException("oldValue");
if (String.Empty == oldValue)
throw new ArgumentException("oldValue");
int lenPattern = oldValue.Length;
int idxPattern = -1;
int idxLast = 0;
StringBuilder result = new StringBuilder(original.Length);
while (true)
{
idxPattern = original.IndexOf(oldValue, idxPattern + 1, comparisonType);
if (idxPattern < 0)
{
result.Append(original, idxLast, original.Length - idxLast);
break;
}
result.Append(original, idxLast, idxPattern - idxLast);
result.Append(newValue);
idxLast = idxPattern + lenPattern;
}
return result.ToString();
}
}
}