Skip to content

Instantly share code, notes, and snippets.

@sliekens
Last active August 29, 2015 14:27
Show Gist options
  • Save sliekens/f6d37739f4167dc7a91f to your computer and use it in GitHub Desktop.
Save sliekens/f6d37739f4167dc7a91f to your computer and use it in GitHub Desktop.
StringExtensions
public static class StringExtensions
{
public static string Replace(this string instance, string oldValue, string newValue, StringComparison comparisonType)
{
if (instance == null)
{
throw new ArgumentNullException("instance");
}
if (string.IsNullOrEmpty(oldValue))
{
return instance;
}
int pos = -1;
while (-1 != (pos = instance.IndexOf(oldValue, pos + 1, comparisonType)))
{
// TODO: compare performance with StringBuilder
instance = instance.Remove(pos, oldValue.Length).Insert(pos, newValue);
}
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment