简体   繁体   English

Java:用HTML段落替换每一行

[英]Java: replacing each line with an HTML paragraph

I'm writing a text to HTML converter. 我正在写文本到HTML转换器。 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 ? 您能否建议一些在Java中可以使用的String替换/正则表达式?
Thanks 谢谢

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

Assuming, 假设,

  1. line delimitter is "\\n". 行定界符为“ \\ n”。
  2. One line is one paragraph. 一行是一段。
  3. The end of txtFileContent is not "\\n" txtFileContent的结尾不是“ \\ n”

Hope this help 希望这个帮助

Try using StringEscapeUtils.escapeHtml and then adding the tags you want at the beginning end. 尝试使用StringEscapeUtils.escapeHtml ,然后在开头添加所需的标签。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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