当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在asp.NET中字符串替换的五种方法

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 在asp.NET中字符串替换的五种方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 74 ::
收藏到网摘: n/a

asp.NET中字符串替换方法小结 1:使用String.Replace函数替换,但不支持大小写。
2:正则System.Text.Regex替换,用RegExpOption修改是否支持大小写。
3:在小数据的情况下,使用String.SubString和+可以实现间接替换。
4:导入MicrosoftVisualBasicRunTime(Microsoft.VisualBasic.DLL)使用Strings.Replace速度很快。
5:参照反射Reflector.FileDisassembler配合Strings.SplitandStrings.Join等实现,速度同5。
一下介绍一种算法,类似KMP算法。有兴趣的参照研究下。
以下为引用的内容:
复制代码 代码如下:
测试
以下为引用的内容:
复制代码 代码如下:

staticvoidMain(string[]args)
{
  stringsegment="AaBbCc";
  stringsource;
  stringpattern="AbC";
  stringdestination="Some";
  stringresult="";
  
  constlongcount=1000;
  StringBuilderpressure=newStringBuilder();
  HiPerfTimertime;
  for(inti=0;i<count;i++)
  {
    pressure.Append(segment);
  }
  source=pressure.ToString();
  GC.Collect();
  //regexp
  time=newHiPerfTimer();
  time.Start();
  for(inti=0;i<count;i++)
  {
    result=Regex.Replace(source,pattern,
         destination,RegexOptions.IgnoreCase);
  }
  time.Stop();
  Console.WriteLine("regexp  ="+time.Duration+"s");
  GC.Collect();
  //vb
  time=newHiPerfTimer();
  time.Start();
  for(inti=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=newHiPerfTimer();
  time.Start();
  for(inti=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=newHiPerfTimer();
  time.Start();
  for(inti=0;i<count;i++)
  {
    result=Test.ReplaceEx(source,pattern,destination);
  }
  time.Stop();
  Console.WriteLine("ReplaceEx="+time.Duration+"s");
  GC.Collect();
  //Replace
  time=newHiPerfTimer();
  time.Start();
  for(inti=0;i<count;i++)
  {
    result=source.Replace(pattern.ToLower(),destination);
  }
  time.Stop();
  Console.WriteLine("Replace ="+time.Duration+"s");
  GC.Collect();
  //sorry,twoslow:(
  /*//substring
  time=newHiPerfTimer();
  time.Start();
  for(inti=0;i<count;i++)
  {
    result=StringHelper.ReplaceText(source,pattern,
         destination,StringHelper.CompareMethods.Text);
  }
  time.Stop();
  Console.WriteLine("substring="+time.Duration+":");
  GC.Collect();
  //substringwithstringbuilder
  time=newHiPerfTimer();
  time.Start();
  for(inti=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?¢stringsegment="abcaBc";
regexp=3.75481827997692s
vb=1.52745502570857s
vbReplace=1.46234256029747s
ReplaceEx=0.797071415501132s!!!<FONTcolor=gray>Replace=0.178327413120941s</FONT>
//ReplaceEx>vbReplace>vb>regexp
2?¢stringsegment="abcaBcabC";
regexp=5.30117431126023s
vb=2.46258449048692s
vbReplace=2.5018721653171s
ReplaceEx=1.00662179131705s!!!
<FONTcolor=gray>Replace=0.233760994763301s</FONT>
//ReplaceEx>vb>vbReplace>regexp
3?¢stringsegment="abcaBcabCAbc";
regexp=7.00987862982586s
vb=3.61050301085753s
vbReplace=3.42324876485699s
ReplaceEx=1.14969947297771s!!!
<FONTcolor=gray>Replace=0.277254511397398s</FONT>
//ReplaceEx>vbReplace>vb>regexp
4?¢stringsegment="ABCabcAbCaBcAbcabCABCAbcaBC";
regexp=13.5940090151123s
vb=11.6806222578568s
vbReplace=11.1757614445411s
ReplaceEx=1.70264153684337s!!!(mygod!)
<FONTcolor=gray>Replace=0.42236820601501s</FONT>
//ReplaceEx>vbReplace>vb>regexp

查看程序的Block在:
以下为引用的内容:
复制代码 代码如下:

stringupperString=original.ToUpper();
stringupperPattern=pattern.ToUpper();

如果需要敏感,就免了这2行。
解释:先建一个char[]类型的变量采访替换后的字符,其大小就是最大可能被替换的字符,例如ABABAB,替换AB成C,其获取过程就是ABABAB最大可能包括的AB的数目乘以AB多于C的数目,
以下为引用的内容:
  char[]chars=newchar[original.Length+Math.Max(0,inc)];
  ,inc不一定大于零。
然后循环,用IndexOf索引。赋值。。。判断,返回。