使用Java如何去掉文本中的空行呢?
注意:windows中的换行符是:\r\n,linux系统中的换行符是:\n
- /***
- * delete CRLF; delete empty line ;delete blank lines
- *
- * @param input
- * @return
- */
- private static String deleteCRLFOnce(String input) {
- if (ValueWidget.isHasValue(input)) {
- return input.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1").replaceAll("^((\r\n)|\n)", "");
- } else {
- return null;
- }
- }
- /**
- * delete CRLF; delete empty line ;delete blank lines
- *
- * @param input
- * @return
- */
- public static String deleteCRLF(String input) {
- input = SystemUtil.deleteCRLFOnce(input);
- return SystemUtil.deleteCRLFOnce(input);
- }
对外公开的方法是:public static String deleteCRLF(String input)
见附件中的com.common.util.SystemUtil