// 替换字符串
public String replace(String source, String oldString, String newString) {
StringBuffer output = new StringBuffer();
int lengthOfSource = source.length();// 源字符串的长度
int lengthOfOld = oldString.length();// 老字符串的长度
int posStart = 0;// 开始搜索位置
int pos;// 搜索到的位置
while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));
output.append(newString);
posStart = pos + lengthOfOld;
} if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
}
return output.toString();
}

解决方案 »

  1.   

    JDK自带的类和方法一般比自己编写的普通类和方法的效率高出80%,那是教材上说的,所以JAVA程序员能力的高低在于对JAVA以有类库的掌握程度.
      

  2.   

    replaceAll()不能处理特殊字符.
    不在太一味地相信JDK的方法.所以分不能随便给.
      

  3.   

    to bbaiyun 
    replaceAll()不能处理特殊字符. 能详细一点吗?什么样的特殊字符?
      

  4.   

    replaceAll 是支持正则表达式的,所以* . 等就存在转义的问题
    使用replace则可以实现全部替换,是不支持正则的