我正在尝试从我的文本中删除所有新行或返回但是我在执行此操作时遇到了很多麻烦。即使我确认新线已被删除,它们仍然会在输出中显示。我做错了什么?
这是我要解析的html文本: ** longDescription“:”切断加拿大AA或USDA选择等级或更高 13.21 / kg的“*
String flyerHTML = sbFlyer.toString();
System.out.println(flyerHTML.contains("\n"));
flyerHTML = flyerHTML.replaceAll("\\r\\n|\\r|\\n", " ");
System.out.println(flyerHTML.contains("\n"));
System.out.println();
while (flyerHTML.contains("\"longDescription\":")) {
String longDescription = "";
// LONG DESCRIPTION
flyerHTML = flyerHTML.substring(flyerHTML.indexOf("\"longDescription\":") + 18);
if (flyerHTML.startsWith("null")) longDescription = "null";
else longDescription = StringEscapeUtils.unescapeHtml4(flyerHTML.substring(1, flyerHTML.indexOf(",") - 1));
System.out.println("LONG DESCRIPTION = " + longDescription);
System.out.println("");
}
答案 0 :(得分:1)
为什么不在循环中添加替换?
while (flyerHTML.contains("\"longDescription\":")) {
String longDescription = "";
// LONG DESCRIPTION
flyerHTML = flyerHTML.substring(flyerHTML.indexOf("\"longDescription\":") + 18);
if (flyerHTML.startsWith("null")) longDescription = "null";
else longDescription = StringEscapeUtils.unescapeHtml4(flyerHTML.substring(1, flyerHTML.indexOf(",") - 1));
longDescription = longDescription.replaceAll("\\r\\n|\\r|\\n", " ");
System.out.println("LONG DESCRIPTION = " + longDescription);
System.out.println("");
}
答案 1 :(得分:0)
您的文字可能包含其他行终结符字符。根据{{3}} 行结束符是一个或两个字符的序列,用于标记输入字符序列的一行的结尾。以下被认为是行终止符: