欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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