自己写的一个replaceAll方法时间:2021-07-18 13:56:45在J2ME中只提供了public String replace(char oldChar,char newChar)方法,不像J2SE中提供了public String replaceAll(String regex,String replacement)方法。由于项目需要自己写了一个: public static String replaceAll(String src,String oldStr,String newStr){ int oldStrLength = oldStr.length(); StringBuffer dest = new StringBuffer(); int index = src.indexOf(oldStr); if(index==-1){ dest.append(src); }else{ dest.append(src.substring(0, index)); } while(index!=-1){ dest.append(newStr); int beforeIndex = index ; index = src.indexOf(oldStr,index+oldStrLength); if(index==-1){ dest.append(src.substring(beforeIndex+oldStrLength)); }else{ dest.append(src.substring(beforeIndex+oldStrLength,index)); } } return dest.toString() ; }