例如:
"1
2
3" 转化成 :
“1
2
3”
通过正则表达式去转换。
代码如下:
/**
* 将字符串中的连续的多个换行缩减成一个换行
* @param str 要处理的内容
* @return 返回的结果
*/
public static String replaceLineBlanks(String str) {
String result = "";
if (str != null) {
Pattern p = Pattern.compile("(\r?\n(\\s*\r?\n)+)");
Matcher m = p.matcher(str);
result = m.replaceAll("\r\n");
}
return result;
}