Created
August 6, 2014 03:29
-
-
Save scorta/3072799db8db94d67f29 to your computer and use it in GitHub Desktop.
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
private static string ReplaceEx(string original, string pattern, string replacement) | |
{ | |
int count, position0, position1; | |
count = position0 = position1 = 0; | |
string upperString = original.ToUpper(); | |
string upperPattern = pattern.ToUpper(); | |
int inc = (original.Length / pattern.Length) * | |
(replacement.Length - pattern.Length); | |
char[] chars = new char[original.Length + Math.Max(0, inc)]; | |
while ((position1 = upperString.IndexOf(upperPattern, | |
position0)) != -1) | |
{ | |
for (int i = position0; i < position1; ++i) | |
chars[count++] = original[i]; | |
for (int i = 0; i < replacement.Length; ++i) | |
chars[count++] = replacement[i]; | |
position0 = position1 + pattern.Length; | |
} | |
if (position0 == 0) return original; | |
for (int i = position0; i < original.Length; ++i) | |
chars[count++] = original[i]; | |
return new string(chars, 0, count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment