http://www.csdn.net/Expert/TopicView1.asp?id=714396

解决方案 »

  1.   

    Java1.4中的字符串替换效率太差,我试过我的实现要快一倍
    如需优化,你可以使用正则表达式
    你的这个问题比较适合使用正则表达式
      

  2.   

    StringTokenizer
    这个类在哪个路径,就是说要import什么?
      

  3.   

    String dest = "http://www.csdn.net/post?var1=%var1%&var2=%var2%;");
    RE re = new RE( "%var1%" );
    String out = re.subst( dest, "xxx" );
    这是Regexp的实现。
      

  4.   

    String dest = "http://www.csdn.net/post?var1=%var1%&var2=%var2%;");
    RE re = new RE( "%var1%" );
    String out = re.subst( dest, "xxx" );
    这是Regexp的实现。
    “RE”是什么类?在哪个包里?
      

  5.   

    public class STR {
     public static void main(String[] args) {
        String s = new String("http://www.csdn.net/post?var1=%var1%&var2=%var2%;");
        String x = "%xxx%";
        String y = "%yyy%";
        int i = s.indexOf("%var1%");
        String s1 = s.substring(0,i)+x;
        String s2 = s.substring(i+6);
        System.out.println(s1);
        System.out.println(s2);
        int j = s2.indexOf("%var2%");
        String s3 = s2.substring(0,j)+y+s2.substring(j+6);
        String s4 = s1+s3;
        System.out.println(s);//原字符串
        System.out.println(s4);//新字符串
     }
    }自个慢慢看吧
      

  6.   

    晕,写完以后就n个人回贴了
    String类好像没自动替换的方法吧
      

  7.   

    btw:我没有对若i=-1和j=-1时的处理
    也就是当string里面不存在匹配的字串时没有处理
      

  8.   

    RE在Apache的Regexp中
    jakarta.apache.org
    --呵呵,最近给人打得最多的好像就这个URL:)
      

  9.   

    咦?!刚才回的怎么不见了
    RE在Apache的Regexp中
    jakarta.apache.org
      

  10.   

    package untitled3;public class MyClass1 {  public MyClass1() {
      }
      /**
       * 使用replaceString替换targetString中的matchString
       * @param targetString 源字符串
       * @param matchString 匹配子串
       * @param replaceString 替换字符串
       * @return 处理后的源字符串
       */
      public final static String replaceString( String target,
                                                  String match,
                                                  String replace)
      {
        int first=target.indexOf(match);
        int last=first+match.length();
        int targetLength=target.length();
        /*
        System.out.println("fi="+first);
        System.out.println("li="+last);
        System.out.println("tL="+targetLength);
        */
        String replacedString=new String("");;//结果存放在这个变量中
        if(first==-1){System.out.println("No match.");return target;}
        else
        {
          replacedString=target.substring(0,first);
          replacedString=replacedString.concat(replace);
          replacedString=replacedString.concat(target.substring(last,targetLength));
        }
        return replacedString;
      }
      public static void main(String argv[])
      {
        String s=new String("0123456789");
        String m=new String("78");
        String r=new String("abcaaaaaaadefghijklm");
        System.out.println(s);
        System.out.println(m+"->"+r);
        s=replaceString(s,m,r);
        System.out.println(s);
      }
    }
    //////////////////////////////////////////////////
    谁的函数有我现在的好的,或者可以把我的做得好一点的话。我那50分就分给谁了。(另外50分给上面的“alphazhao(绿色咖啡) ”=30和: wangwenyou(王文友)=20)。
    因为我的思路是被他们提醒起来的,虽然我曾经做过这样的替换,但是现在一直头脑很热,想不起来怎么做了,而当时又由于头脑很清以至于没有把他看做一个问题,就没有把他写做函数留作后用,今天就不大想起来了。
    刚才受绿色咖啡的实现中的函数的启发,写了上面的实现过程,且借鉴了王文友的算法和格式。因此决定给分,当不是全分,因为绿色咖啡没有答的全对,不是说他的以外处理,而是他没有看清题意,他的结果不是我想要的。
    我已经坐在电脑边一个下午了,从2:30到现在20点了吧。我是该做一做我的伲称了,我伲称本来就是提醒我要“想不出来,就出去转转”。
      

  11.   

    考也不算,只是我当时确实是想不起来了,不过你的哪个程序我还没有看懂,特别是那个StringTokenizer我没有用过。你们不要再说三道四了。我结帖。
      

  12.   

    redv的方法至少应该把函数写成一个递归调用过程,你的只能替换一次字符串