批量替换字符串,比如文章中hello,hi,nihao,welcome,chilema,都要替换为Comment vas-tu

来源:百度知道 编辑:UC知道 时间:2024/05/02 18:39:29
就是批量替换,可以不用循环做吗,for循环10000次是不是影响服务器速度啊,
批量替换字符串,比如文章中hello,hi,nihao,welcome,chilema,都要替换为Comment vas-tu
正则,或特殊方法实现都可以,要javascript或c#的

用正则吧,比较快的
string pattern = "[hello|hi|nihao|welcome|chilema]";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string result = reg.Replace("你的原字符串", "Comment vas-tu");
//result 就是最后的结果

javascript如下:
var str="";//你的文章
var reg=/(hello)|(hi)|(nihao)|(welcome)|(chilema)/gi;
str=str.replace(reg,"Comment vas_tu");

c#的话,在使用前要加上using System.Text.RegularExpressions;
语句如下
string str=@"";//你的文章
System.Text.RegularExpressions.Regex.Replace(str,"(hello)|(hi)|(nihao)|(welcome)|(chilema)","Comment vas_tu");

比如该文章的字符串为:string a="sdfsadfadfafafadfadfsafdad.......";
转换后:a=a.Replace("hello","Comment vas-tu").Replace("hi","Comment vas-tu").Replace("nihao&