开发者

Encoding in Java

开发者 https://www.devze.com 2023-01-10 16:54 出处:网络
I have a string with special characters in Java. \"[^\\\\\"]*\\\\\" I want to convert it to HTML entities like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消