I have a string with special characters in Java.
"[^\\"]*\\"
I want to convert it to HTML entities like this:
"[^\\"]*\\"
How this could 开发者_高级运维be achieved in Java?
Apache Commons Lang includes a helper for encoding HTML. StringEscapeUtils.escapeHtml() should do the trick. According to the javadocs it "Supports all known HTML 4.0 entities, including funky accents".
Use String.replace()
as needed:
"[^\\\"]*\\".replace("\"", """);
You need to parse the input string character by character and look for particular ones that you want to convert. When you find a match, simply replace that character with the HTML entity.
精彩评论