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.
Apache Commons has a StringEscapeUtils
class which is able to escape (and unescape) in the following formats:
- CSV
- HTML
- Java
- JavaScript
- SQL
- XML
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.
You can use OWASP's Enterprise Security API (Java Edition)
精彩评论