unbescape library
You may use the library unbescape for powerful, fast, and easy escape/unescape operations in Java.
Example
Add the dependency into the pom.xml
file:
<dependency>
<groupId>org.unbescape</groupId>
<artifactId>unbescape</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
The usage:
import org.unbescape.html.HtmlEscape;
import org.unbescape.html.HtmlEscapeLevel;
import org.unbescape.html.HtmlEscapeType;
<โฆ>
final String inputString = "\uD83C\uDF7F";
final String escapedString = HtmlEscape.escapeHtml(
inputString,
HtmlEscapeType.HEXADECIMAL_REFERENCES,
HtmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT
);
// Here `escapedString` has the value: `🍿`.
For your use case, probably, either HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
or HtmlEscapeType.HTML5_NAMED_REFERENCES_DEFAULT_TO_HEXA
should be used instead of HtmlEscapeType.HEXADECIMAL_REFERENCES
.