对于多行文本,你直接通过replace,replaceAll是不能将空行删除的,你需要遍历这些行,对每行文本进行操作,最后把返回新的文本才行。
public static String removeMultiRowBlank(String msg) {
InputStream is = new ByteArrayInputStream(msg.getBytes());
BufferedReader br = null;
String tmp;
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
try {
br = new BufferedReader(new InputStreamReader(is, "utf-8"));
while ((tmp = br.readLine()) != null) {
if (StringUtils.isBlank(tmp)) ;
else {
stringBuilder.append(tmp + "\n");
i++;
System.out.println(i);
}
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
通过上面的方法,将把上图中第一行(空行)删除,并返回处理后的字符串。