Java 删除换行
程序员文章站
2022-05-31 16:38:22
...
Java 删除换行:
/***
* Delete all spaces
*
* @param input
* @return
*/
public static String deleteAllCRLF(String input) {
return input.replaceAll("((\r\n)|\n)[\\s\t ]*", " ").replaceAll(
"^((\r\n)|\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 all spaces
*
* @param input
* @return
*/
public static String deleteAllCRLF(String input) {
return input.replaceAll("((\r\n)|\n)[\\s\t ]*", "").replaceAll(
"^((\r\n)|\n)", "");
}
public static String CRLF2Blank(String input) {
return input.replaceAll("((\r\n)|\n)[\\s\t ]*", " ").replaceAll(
"^((\r\n)|\n)", " ").replaceAll("[ ]{2,}", " ");//注意:replace不支持正则表达式
}
/**
* delete CRLF; delete empty line ;delete blank lines
*
* @param input
* @return
*/
public static String deleteCRLF(String input) {
input = SystemHWUtil.deleteCRLFOnce(input);
return SystemHWUtil.deleteCRLFOnce(input);
}
SystemHWUtil 见附件中的com.common.util.SystemHWUtil
上一篇: PHP代码优化的集合
下一篇: 七、删除与比较文件