java实现将文本中多余的空格去掉

如:解决文本中通过多个空格达到换行的目的,我们将此处的多处空格替换为换行符。
/**
* 将文本数据按照换行符进行分割
* @param content
* @return
*/
public static String deleteBlankSpace(String content) {
if(content == null || “”.equals(content)) {
return “”;
}
StringBuilder resultSB = new StringBuilder();
//TODO
String[] lines = content.split("\n");
//int blankCount = 0;

	for(String line : lines) {
		if(line == null) {
			continue;
		}
		String lineTrim = line.trim();
		resultSB.append(dealSpace4OneLine(lineTrim)).append("\n");
	}
	
	resultSB.deleteCharAt(resultSB.toString().length()-1);
	return resultSB.toString();

	
}

/**
 * 将一行数据中多余的空格去除
 * @param line
 * @return
 */
private static String dealSpace4OneLine(String line) {
	if(line == null || "".equals(line)) {
		return "";
	}
	
	int spaceCount = 0;
	int firstPosition = 0;
	StringBuilder sb = new StringBuilder();
	for(int i=0;i

你可能感兴趣的:(Java学习笔记)