str.valueOf("two"))!=-1)改为str.indexOf("two"))!=-1)

解决方案 »

  1.   

    楼上说的有理:)在jdk1.4里边,就直接有
    String.replaceAll()方法。public String replaceAll(String regex,String replacement)
      

  2.   

    从你贴出来的错误提示来看,应该是str.valueOf()的问题。应该为str.indexOf()。
    但从程序来看,str=str.substring(0,xxx)+"three"+str.substring(xxx+3);这句也是不对的。String型应该是不可改变的。应该用StringBuffer型。代码如下:
    StringBuffer strBuf = new StringBuffer().append("There are two tigers live in two different forest!");int xxx=0;
    System.out.println("替换前的字符串为:"+strBuf);
    while((xxx=strBuf.indexOf("two"))!=-1)strBuf = strBuf.replace(10,13,"Three");System.out.println("替换后的字符串为:"+strBuf);
      

  3.   

    string型变量也同样可以改变!
    用indexOf就可以返回字符下标
      

  4.   

    str.valueOf("two"))!=-1)改为str.indexOf("two"))!=-1)
      

  5.   

    up and test
    hehe
    长恨此身非我有
    何时忘却营营
    夜阑风静谷纹平
    小舟从此逝
    江海寄余生