jsoup to strip only html tags not new line character?

后端 未结 2 1402
耶瑟儿~
耶瑟儿~ 2021-01-16 11:24

I have below content in Java where I want to strip only html tags but not new line characters

test1 test2 test 3

//lin
2条回答
  •  不思量自难忘°
    2021-01-16 11:44

    You get a single line because text() remove all whitepace characters. But you can use a StringBuilder and insert each line there:

    final String html = "

    test1 test2 test 3

    " + "

    test4

    "; Document doc = Jsoup.parse(html); StringBuilder sb = new StringBuilder(); for( Element element : doc.select("p") ) { /* * element.text() returns the text of this element (= without tags). */ sb.append(element.text()).append('\n'); } System.out.println(sb.toString().trim());

    Output:

    test1 test2 test 3
    test4
    

提交回复
热议问题