一个字符串
string str=“abcdefghijklmnopqrs[:ad]tuv[~ed]wxy[-ad]zabcdefghij[%de]klmnopqr[c@d]stuvwxyzabcdef[ab*]ghijklmnopqr[ee]stuvwxyz[zo]abcdefghi[d]jklmnop[d]qrstu[d]vwx[d]yz……要把其中的类似 [~ed] 的字符替换成相应的超链接 。如[~ed] 替换成:http:hello.com ,但是如[~ed]的字符在这个字符串中有两百个,如果用replace 的话 就得 str=str.replace("[~ed]","http:hello.com").replace(……).replace(……)…… 这样效果肯定会很低,有没有好的方法提高 它 的性能 ?
string str=“abcdefghijklmnopqrs[:ad]tuv[~ed]wxy[-ad]zabcdefghij[%de]klmnopqr[c@d]stuvwxyzabcdef[ab*]ghijklmnopqr[ee]stuvwxyz[zo]abcdefghi[d]jklmnop[d]qrstu[d]vwx[d]yz……要把其中的类似 [~ed] 的字符替换成相应的超链接 。如[~ed] 替换成:http:hello.com ,但是如[~ed]的字符在这个字符串中有两百个,如果用replace 的话 就得 str=str.replace("[~ed]","http:hello.com").replace(……).replace(……)…… 这样效果肯定会很低,有没有好的方法提高 它 的性能 ?
{
str=str.replace("[~ed]","http:hello.com");
}
有这个说法???你试验过没有??
str=str.replace("[~ed]","http:hello.com");
这样不行????
<script language="JavaScript"> var s = "testtest" ;
//第二个参数中的 g 表示全部匹配,i表示忽略大小写
var regS = new RegExp("test","gi"); alert(s.replace(regS,"Hello")); //全部替换
</script>
呵呵, 做出来就是了,这个性能和int m=1+1;的性能差不错的.
str=str.replace("[~ed]","http:hello.com");
str=str.replace("[!ed]","http:hello1.com");
str=str.replace("[#ed]","http:hello2.com");
str=str.replace("[%ed]","http:hello3.com");
str=str.replace("[)ed]","http:hello4.com");
str=str.replace("[(ed]","http:hello5.com");
str=str.replace("[_ed]","http:hello6.com");
…… 一共两百行。。我觉得这样的操作效率会很低 ,求指点!
认真看没有???replace一次就全部替换了,
谁要你写两百次????6楼、8楼的,
你都认真看了吗?????????????????
str = Regex.Replace(str, @"\[.*?ed\]", "http:hello.com", RegexOptions.IgnoreCase);
让不要大家进来,
直接说:“xxx,来帮我解决这个问题”
我还懒得费这个神!!!!
这个没有问题的,如果还有别的替换对象,如此而已
str=str.replace("[!ed]","http:hello1.com");
str=str.replace("[#ed]","http:hello2.com");
str=str.replace("[%ed]","http:hello3.com");
str=str.replace("[)ed]","http:hello4.com");
str=str.replace("[(ed]","http:hello5.com");
str=str.replace("[_ed]","http:hello6.com");按照你这个表情设计,,基本无解,,不规律,,比如: [[~ed_1]] ==> http:hello1.com
总要有规律吧?
//建立字典
System.Collections.Generic.Dictionary<string, string> replaces = new Dictionary<string, string>();
replaces.Add(@"~ed", @"http:hello.com");
//...
string replace;
string[] strings = str.Split('[');//这里用字符分割的方法,当然如果要更高效应该直接循环IndexOf('[', lastIndex)定位
System.Text.StringBuilder newString = new System.Text.StringBuilder(strings[0]);
for (int endIndex, length = strings.Length, index = 1; index < length; index++)
{
if ((endIndex = strings[index].IndexOf(']')) == -1 || replaces.TryGetValue(strings[index].Substring(0, endIndex), out replace)) newString.Append('[').Append(strings[index]);
else newString.Append(replace).Append(strings[index].Substring(endIndex + 1));
}
return newString.ToString();