java 中会经常处理字符串,需要删除其中的空行,而且要删除仅包含Tab、空格的空行。
范例如下:
@Test
public void deleteBlankLines4(){
String input="aa\r\n\t\r\nbb\r\n\r\n\r\ncc\r\naa\n\nbb\n\n\ncc\n";
System.out.println(input);
System.out.println("--------------------------------------");
System.out.println(deleteCRLF(input));
}
/***
* delete CRLF; delete empty line ;delete blank lines
*
* @param input
* @return
*/
private static String deleteCRLFOnce(String input) {
return input.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1");
}
/**
* delete CRLF; delete empty line ;delete blank lines
*
* @param input
* @return
*/
public static String deleteCRLF(String input) {
input=deleteCRLFOnce(input);
return deleteCRLFOnce(input);
}
测试方法的运行结果如下:
aa
bb
cc
aa
bb
cc
--------------------------------------
aa
bb
cc
aa
bb
cc