字符串中的转义字符排除html标记

时间:2015-04-30 12:48:26

标签: java html string escaping

有没有办法如何在包含html标签的字符串中的Java字符中进行转义,我想不要逃避?例如在字符串中我有

<h1>a b c d ö</h1>

我需要得到:

<h1>a b c d &ouml</h1>

2 个答案:

答案 0 :(得分:1)

请参阅:

https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html#escapeHtml4%28java.lang.String%29

  public static void main(String[] args) {
    String s = "a b c d ö";
    System.out.println(StringEscapeUtils.escapeHtml4(s));
  }

答案 1 :(得分:0)

选中此项:

g++

println textOut:String textIn = "<h1>a b c d ö</h1>"; String textOut = StringEscapeUtils.escapeHtml4(textIn);


对于xml或标签:

&lt;h1&gt;a b c d &ouml;&lt;/h1&gt

println :textOut String textIn = "<h1>a b c d ö</h1>"; String textOut = StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeHtml4(textIn));