转义引号的 Java 库
我经常必须转义字符串中的许多引号。有没有一个库提供这样的字符串函数。它很容易编写,但是具有许多不同的转义功能/选项的东西会很方便。
I often have to escape many quotation marks in a String. Is there a library that provides String functions such as this. It's easy enough to write, but something that had lots of different features/options for escaping would be handy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Apache Commons 有一个
StringEscapeUtils
能够以以下格式转义(和取消转义)的类:Apache Commons has a
StringEscapeUtils
class which is able to escape (and unescape) in the following formats:这取决于您转义引号的上下文。通常,它应该由特定于任务的库来处理。例如,如果要转义引号以便在 SQL 字符串中使用,则应该使用参数占位符而不是连接字符串。如果要转义引号以便在 JSP 中使用,则应该使用
来处理引号和其他 HTML 实体。It depends on the context in which you're escaping the quotation marks. Usually, it should be handled by a task-specific library. For example, if you're escaping quotes for use in an SQL string, you should be using parameter placeholders instead of concatenating the string. If you're escaping quotes for use in a JSP, you should use
<c:out>
which will deal with quotes and other HTML entities.您可以使用 OWASP 的企业安全 API(Java 版)
You can use OWASP's Enterprise Security API (Java Edition)