中易网

.net中如何替换字符串

答案:4  悬赏:50  
解决时间 2021-01-19 23:30
  • 提问者网友:山高云阔
  • 2021-01-18 23:08
.net中如何替换字符串
最佳答案
  • 二级知识专家网友:持酒劝斜阳
  • 2021-01-18 23:54
1:使用String.Replace函数替换。

2:正则System.Text.Regex替换,用RegExpOption修改是否支持大小写。

3:在小数据的情况下,使用String.SubString和+可以实现间接替换。

4:导入MicrosoftVisualBasicRunTime(Microsoft.VisualBasic.DLL)使用Strings.Replace速度很快。

5:参照反射Reflector.FileDisassembler配合Strings.SplitandStrings.Join等实现。

以下为引用的内容:

privatestaticstringReplaceEx(stringoriginal,
          stringpattern,stringreplacement)
{
  intcount,position0,position1;
  count=position0=position1=0;
  stringupperString=original.ToUpper();
  stringupperPattern=pattern.ToUpper();
  intinc=(original.Length/pattern.Length)*
       (replacement.Length-pattern.Length);
  char[]chars=newchar[original.Length+Math.Max(0,inc)];
  while((position1=upperString.IndexOf(upperPattern,
                   position0))!=-1)
  {
    for(inti=position0;i      chars[count++]=original[i];
    for(inti=0;i      chars[count++]=replacement[i];
    position0=position1+pattern.Length;
  }
  if(position0==0)returnoriginal;
  for(inti=position0;i    chars[count++]=original[i];
  returnnewstring(chars,0,count);
}
全部回答
  • 1楼网友:低血压的长颈鹿
  • 2021-01-19 02:50
string.replace or regularexpression
  • 2楼网友:洒脱疯子
  • 2021-01-19 02:06
用记事本打开XML文件 CTRL+F 替换所有敏感字符
  • 3楼网友:山河有幸埋战骨
  • 2021-01-19 00:32
string str="abcdefgh";
假如b为敏感字符,则
str.Replace("b","**");
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息