我有以下代码:
public class NewClass {
public String noTags(String str){
return Jsoup.parse(str).text();
}
public static void main(String args[]) {
String strings="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN \">" +
"<HTML> <HEAD> <TITLE></TITLE> <style>body{ font-size: 12px;font-family: verdana, arial, helvetica, sans-serif;}</style> </HEAD> <BODY><p><b>hello world</b></p><p><br><b>yo</b> <a href=\"http://google.com\">googlez</a></p></BODY> </HTML> ";
NewClass text = new NewClass();
System.out.println((text.noTags(strings)));
}
我得到了结果:
hello world yo googlez
但我想打破界限:
hello world
yo googlez
我看过 jsoup 的 TextNode#getWholeText() 但我不知道如何使用它。
如果在我解析的标记中有 <br>
,我怎样才能在结果输出中换行?
原文由 Billy 发布,翻译遵循 CC BY-SA 4.0 许可协议
保留换行符的真正解决方案应该是这样的:
它满足以下要求: