asp.net efficient substitution of large-capacity characters to implement code _ practical tips

Source: Internet
Author: User
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;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.