1: Replace with the String.Replace function, but not case-supported.
2: Regular System.Text.Regex replacement, with regexpoption modify whether or not to support case.
3: In the case of small data, the use of string.substring and + can achieve indirect replacement.
4: Import microsoftvisualbasicruntime (Microsoft.VisualBasic.DLL) use strings.replace speed quickly.
5: The reference reflection Reflector.filedisassembler cooperate with Strings.SplitandStrings.Join and so on, the speed is same as 5.
This paper introduces an algorithm similar to KMP algorithm. Interested in the study under the reference.
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);
}
Test
static void Main (string[] args)
{
String segment = "AABBCC";
string source;
String pattern = "AbC";
String destination = "Some";
string result = "";
Const Long Count = 1000;
StringBuilder pressure = new StringBuilder ();
Hiperftimer time;
for (int i = 0; i < count; i++)
{
pressure. Append (segment);
}
Source = pressure. ToString ();
Gc. Collect ();
Regexp
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = Regex.Replace (source, pattern,
Destination, regexoptions.ignorecase);
}
Time. Stop ();
Console.WriteLine ("RegExp =" + time.) Duration + "s");
Gc. Collect ();
Vb
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = Strings.replace (source, pattern,
Destination, 1,-1, CompareMethod.Text);
}
Time. Stop ();
Console.WriteLine ("VB =" + time.) Duration + "s");
Gc. Collect ();
Vbreplace
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = Vbstring.replace (source, pattern,
Destination, 1,-1, Stringcomparemethod.text);
}
Time. Stop ();
Console.WriteLine ("Vbreplace =" + time.) Duration + "s");/+ result);
Gc. Collect ();
Replaceex
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = Test.replaceex (source, pattern, destination);
}
Time. Stop ();
Console.WriteLine ("Replaceex =" + time.) Duration + "s");
Gc. Collect ();
Replace
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = source. Replace (pattern. ToLower (), destination);
}
Time. Stop ();
Console.WriteLine ("Replace =" + time.) Duration + "s");
Gc. Collect ();
Sorry, two slow:(
/*//substring
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = Stringhelper.replacetext (source, pattern,
Destination, StringHelper.CompareMethods.Text);
}
Time. Stop ();
Console.WriteLine ("substring =" + time.) Duration + ":");
Gc. Collect ();
SUBSTRING with StringBuilder
Time = new Hiperftimer ();
Time. Start ();
for (int i = 0; i < count; i++)
{
result = STRINGHELPER.REPLACETEXTB (source, pattern,
Destination, StringHelper.CompareMethods.Text);
}
Time. Stop ();
Console.WriteLine ("substringb=" + time.) Duration + ":");
Gc. Collect ();
*/
Console.ReadLine ();
}
1¡¢string segment = "ABCABC";
RegExp = 3.75481827997692s
vb = 1.52745502570857s
Vbreplace = 1.46234256029747s
Replaceex = 0.797071415501132s!!! <font color=gray>replace = 0.178327413120941s </FONT>
Replaceex > Vbreplace > VB > RegExp
2¡¢string segment = "ABCABCABC";
RegExp = 5.30117431126023s
vb = 2.46258449048692s
Vbreplace = 2.5018721653171s
Replaceex = 1.00662179131705s!!!
<font color=gray>replace = 0.233760994763301s </FONT>
Replaceex > vb > Vbreplace > RegExp
3¡¢string segment = "ABCABCABCABC";
RegExp = 7.00987862982586s
vb = 3.61050301085753s
Vbreplace = 3.42324876485699s
Replaceex = 1.14969947297771s!!!
<font color=gray>replace = 0.277254511397398s </FONT>
Replaceex > Vbreplace > VB > RegExp
4¡¢string segment = "ABCABCABCABCABCABCABCABCABC";
RegExp = 13.5940090151123s
vb = 11.6806222578568s
Vbreplace = 11.1757614445411s
Replaceex = 1.70264153684337s!!! (My god!)
<font Color=gray>replace = 0.42236820601501s</font>
Replaceex > Vbreplace > VB > RegExp
View the program's block in:
String upperstring = original. ToUpper ();
String Upperpattern = pattern. ToUpper ();
If you need to be sensitive, avoid these 2 lines.
Explanation: First build a char[] type of variable interview character, its size is the most likely to be replaced by characters, such as Ababab, replace AB into C, the acquisition process is the Ababab maximum number of AB can be multiplied by the number of AB more than C,
char [] chars = new char[original. Length + math.max (0, Inc)];,inc is not necessarily greater than 0.
Then loop, using the IndexOf index. Assign Value ... Judged, returned.