具体来说,替换字符串中特殊XML 字符的最简单和最惯用的方法是什么。例如,转换<Jack & Jill>
为<Jack & Jill>
.
3 回答
事实证明有一种简单的方法可以做到这一点(尽管快速的网络搜索没有发现明显的解决方案):只需使用 method xml.Utility.escape
。
If you are looking to escape the @
sign in a scala.html file, for instance, using Scala/Play, do @@
.
And give the person who provided this answer here How to print @ symbol in HTML with play framework (scala) an upvote.
I came across this page while looking for the above answer, so I just wanted to duplicate it here since other people may end up here as well.
我建议使用该功能org.apache.commons.text.StringEscapeUtils.escapeHtml4
,添加build.sbt
到libraryDependencies += "org.apache.commons" % "commons-text" % "1.9"
(xml.Utility.escape
不逃避例如«
«
)