I need to encode a Java String to display properly as part of an HTML document, e.g. all new lines or tab characters. So I have e.g. "one\ntwo"
, and I want to get something like "one<br>two"
. Do you know any library that would do it for me?
views:
101answers:
3your answer is not related to question as it not convert line breaks to br. pre tag is used normally to show code as it preserves spaces and line breaks.
Adeel
2010-02-25 11:34:15
Its not exactly what I asked about, but does the job well enough!
Grzenio
2010-02-25 13:40:33
+1
A:
You don't need a library for those simple cases. A simple loop over string.replaceAll()
should do the trick.
If you are looking for more fancy conversions (such as done here on SO or in a wiki) you can check out the Java Wikipedia API. Code example here. Although I guess it may be a bit overkill for your needs.
Martin Wickman
2010-02-25 11:34:35
+1
A:
Spring Framework's HtmlUtils.htmlEscape(String)
method should do the trick:
Turn special characters into HTML character references. Handles complete character set defined in HTML 4.01 recommendation.
Adamski
2010-02-25 11:44:28
-1 This just converts characters with a code point > 128, not unprintable characters.
Aaron Digulla
2010-02-25 14:56:16
True, but in reality it's fairly unlikely that a string would contain unprintable characters.
Adamski
2010-02-25 20:37:04