Last active
August 29, 2015 14:27
-
-
Save sliekens/f6d37739f4167dc7a91f to your computer and use it in GitHub Desktop.
StringExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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