从你贴出来的错误提示来看,应该是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);
String.replaceAll()方法。public String replaceAll(String regex,String replacement)
但从程序来看,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);
用indexOf就可以返回字符下标
hehe
长恨此身非我有
何时忘却营营
夜阑风静谷纹平
小舟从此逝
江海寄余生