一个字符串
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(……)……           这样效果肯定会很低,有没有好的方法提高  它 的性能 ?

解决方案 »

  1.   

    str = Regex.Replace(str, @"\[\~ed\]", "http:hello.com", RegexOptions.IgnoreCase);
      

  2.   

    while(str.IndexOf("[~ed]") > -1)
    {
    str=str.replace("[~ed]","http:hello.com");
    }
      

  3.   


    有这个说法???你试验过没有??
     str=str.replace("[~ed]","http:hello.com");
    这样不行????
      

  4.   

    C#  是一次替换完的。你是JS?
     <script language="JavaScript"> var s = "testtest" ;
     
     //第二个参数中的 g 表示全部匹配,i表示忽略大小写
     var regS = new RegExp("test","gi"); alert(s.replace(regS,"Hello")); //全部替换
     </script>
      

  5.   


    呵呵, 做出来就是了,这个性能和int m=1+1;的性能差不错的.
      

  6.   

    呵呵,求高手指点那,
    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");
    ……  一共两百行。。我觉得这样的操作效率会很低  ,求指点!
      

  7.   

    4楼你看没有???
    认真看没有???replace一次就全部替换了,
    谁要你写两百次????6楼、8楼的,
    你都认真看了吗?????????????????
      

  8.   

    c#中的str.replace("","")只需写一次的就可以将str中的指定字符窜替换成自己的字符串。
      

  9.   

    replace也会重新生产一个字符串对象的
      

  10.   


    str = Regex.Replace(str, @"\[.*?ed\]", "http:hello.com", RegexOptions.IgnoreCase);
      

  11.   

    想倒分就明着说,
    让不要大家进来,
    直接说:“xxx,来帮我解决这个问题”
    我还懒得费这个神!!!!
      

  12.   

    事实上,字符并非只有[字符ed],也能可能 是[ed字符]  或者[字符字符](如[ze%])
      

  13.   

    你存储的时候就有问题了吧,既然是表情符号,你存的时候注意一下,[~ed] [!ed],不要简单用一个中括号,用两个这样 ,[[~ed]] [[!ed]],替换的时候就替换[[ 为<img ... ,]] 替换为/>, ~ed不替换,但是那些表情图片的名称和他一致就可以了
      

  14.   

    str = Regex.Replace(str, @"\[\~ed\]", "http:hello.com", RegexOptions.IgnoreCase);
    这个没有问题的,如果还有别的替换对象,如此而已
      

  15.   

    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");按照你这个表情设计,,基本无解,,不规律,,比如:  [[~ed_1]]  ==>  http:hello1.com
    总要有规律吧?
      

  16.   

    可以添加使用StringBuilder提高replace的性能。在客户端显示的时候,使用js替换[字符],或者添加表情的时候保存到数据库为html标签。因为表情字符除了有一对中括号外没有什么规则,正则基本上用不上了。感谢各位前来帮助我的人,非常感谢foren_whb(丰云)对我批评,也感谢kkbac(萝卜) 对我的变相批评,我虽数年前就上csdn,但对里面的一些细节或者规章也不甚了解,我的提问也不是很明确。在csdn里讨论问题从没有过今天这样的热情,呵呵,和大家在一起讨论问题很开心! 就此结贴!
      

  17.   


                //建立字典
                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();