看到了这个方法,类似于JavaScript的str.replaces(function(...){...},"...")这种,代码是转载的,备注下
/**
*
* @param str 待处理的字符串
* @param replacement 处理成的目标字符
* @param ignoreCase 是否忽略大小写
* @return
*/
public static String filterRepetition(String str,String replacement,boolean ignoreCase) {
String regex="(.)\\1+";
regex=ignoreCase?("(?i)"+regex):regex;
Matcher m=Pattern.compile(regex,Pattern.DOTALL).matcher(str);
StringBuffer result=new StringBuffer();
while(m.find()){
m.appendReplacement(result, m.group().replaceAll(".", replacement));
}
m.appendTail(result);
return result.toString();
}