yinjia
yinjia

Reputation: 824

Escape HTML in Languages with Accented Letters

Which html escape method in Java is recommended to use if I don't want it to escape accented characters, for example, in string "Matías", accented í should remain unescaped.

Both StringEscapeUtils.escapeHtml() and Springs's HtmlUtils.htmlEscape() escape these letters.

Upvotes: 3

Views: 2387

Answers (3)

PeeWee2201
PeeWee2201

Reputation: 1534

Using Apache commons-text:

   public static final CharSequenceTranslator ESCAPE_CUSTOM =
         new AggregateTranslator(
                 new LookupTranslator(EntityArrays.BASIC_ESCAPE),
                 new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE)
         );
   ESCAPE_CUSTOM.translate(input);

Upvotes: 1

Hugo G
Hugo G

Reputation: 16546

Using Spring's htmlEscape(String input, String encoding) you can pass an encoding like "UTF-8". According to the JavaDoc the characters won't be escaped if they're in the given encoding (or at least that's how I understand it).

Upvotes: 4

yinjia
yinjia

Reputation: 824

Guava's HtmlEscapers.htmlEscaper().escape(inputString) did the trick without specifying encoding

Upvotes: 2

Related Questions