我用 matcher 实现了 replaceAll() 方法,它用“”替换所有标点符号。但它总是抛出异常:“ java.lang.StringIndexOutOfBoundsException: String index out of range: 6 ”
private static StringBuilder filterPunctuation(StringBuilder sb){
Pattern pattern = Pattern.compile("(\\.)");
Matcher matcher = pattern.matcher(sb);
while(matcher.find()){
sb.replace(matcher.start(), matcher.end(), "");
// if sb.replace(matcher.start(),matcher.end()," "), it wil be right, but I want replace all punction with ""
}
return sb;
}
public static void main(String[] args){
System.out.println(filterPunctuation(new StringBuilder("test.,.")));
}