The general substitution operation is this:
Copy Code code as follows:
Str=str.replace (String one, string two)
It is not difficult to find a problem, if Str to be recycled many times, the next replacement will add the last replaced the content, and the full traversal, if the string two many, the replacement process is like a ladder effect, increasing, so the speed is more and more slow. To solve this problem, we can only find another way to replace this expression.
How to replace this operation with more efficiency? Ideas are as follows:
After each replacement, the replacement is excluded in the next replacement and the replacement content is accumulated.
Copy Code code as follows:
Public Regex Returnmatch (String str)//matching regular
{
Regex R;
R = new Regex (@str, regexoptions.ignorecase);
return R;
}
<summary>
Replace
</summary>
<param name= "Sdetail" > Characters to be processed </param>
<param name= "regex" > Regular expression </param>
<param name= "Replace_str" > What to replace </param>
<returns> processed characters </returns>
public string Replace (string sdetail,string regex)
{
int last_index=0;
String Cut_str=sdetail;
String Return_str= "";
Regex R;
Match m;
R = Returnmatch (regex);
for (M = R.match (sdetail); m.success m = M.nextmatch ())
{
int n=m.groups[0]. length;//Matching length
Cut_str=cut_str. Substring (last_index,cut_str. LENGTH-LAST_INDEX);//Remove the last result
int K=CUT_STR. IndexOf (M.groups[0]. ToString ());/Current Position
String This_v=cut_str. Substring (k,n);//Current matching value
String Str3=cut_str. Substring (0,k+n)//Current resulting value
Return_str+=str3. Replace (m.groups[0). ToString (), Return_item_content (M.groups[0]. ToString ()));
RETURN_STR+=EVN (STR3,M);
last_index=k+n;//records the current matching location
}
if (return_str!= "")
Sdetail=return_str+cut_str. Substring (last_index,cut_str. Length-last_index);
return sdetail;
}
}