// 替换字符串
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();
}
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();
}
不在太一味地相信JDK的方法.所以分不能随便给.
replaceAll()不能处理特殊字符. 能详细一点吗?什么样的特殊字符?
使用replace则可以实现全部替换,是不支持正则的