简体   繁体   中英

Java: replacing each line with an HTML paragraph

I'm writing a text to HTML converter. I'm looking for a simple way to wrap each line of text (which ends with carriage return) with

<p>.....text.....</p>

Can you suggest some String replacement/regular expression that will work in Java ?
Thanks

String txtFileContent = ....;
String htmlContent = "<p>" + txtFileContent.replaceAll("\\n","</p>\\n<p>") + "</p>";

Assuming,

  1. line delimitter is "\\n".
  2. One line is one paragraph.
  3. The end of txtFileContent is not "\\n"

Hope this help

Try using StringEscapeUtils.escapeHtml and then adding the tags you want at the beginning end.

    String escapeHTML = StringEscapeUtils.escapeHtml(inputStr);
    String output = "<p>"+escapeHTML+"</p>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM