Advanced yet easy to use escaping library for Java
Unbescape is a Java library aimed at performing fully-featured and high-performance escape and unescape
operations for:
.properties
filesThis project is stable and production-ready.
Current versions:
This software is licensed under the Apache License 2.0.
org.unbescape
unbescape
String
or char[]
objects are created, and specific optimizations are applied in order to provide maximum performance and reduce Garbage Collector latency (e.g. if a String
has the same content after escaping/unescaping, exactly the same String
object is returned, no copy is made).benchmark.sh
script in theunbescape-tests
repository for specific figures.U+0000
to U+10FFFF
, including characters not representable by only one char in Java (>U+FFFF
).]
,

, etc. (HTML 4 set available too).fj
→ fj
.⎬
).á
).€
for €
(€
) instead of €
).<
, >
, &
, "
and '
.\0
, \b
, \t
, \n
, \v
, \f
, \r
, \"
, \'
, \\
.\v
(U+000B
) will not be used in escape operations (only unescape) because it is/
(as \/
if possible) when it appears after <
, as in </something>
.U+0001
to U+001F
and U+007F
to U+009F
.\xE1
.\u00E1
.\071
. Not supported\b
, \t
, \n
, \f
, \r
, \"
, \\
./
(as \/
if possible) when it appears after <
, as in </something>
.U+0000
to U+001F
and U+007F
to U+009F
.\u00E1
.%HH
).\+
, \;
, \(
, \)
, etc.).\:
not used because of lacking support in Internet Explorer < 8,\_
escaped at the beginning of identifiers for better Internet Explorer 6 support, etc.\E1
) and six-digit forms (\0000E1
).\uFFFF
both when represented in standard form (one char,\20000
) and non-standard (surrogate pair, \D840\DC00
, used by older WebKit browsers)."value"
) if they contain any non-alphanumeric characters."
) by writing them twice: ""
.\b
, \t
, \n
, \f
, \r
, \"
, \'
, \\
. Note \'
will not beU+0001
to U+001F
and U+007F
to U+009F
.\u00E1
.\071
. Not supported.properties
File Escape/Unescape
\t
, \n
, \f
, \r
, \\
. When escaping .properties
\
, \:
and \=
will be applied too.U+0001
to U+001F
and U+007F
to U+009F
.\u00E1
.HTML escape and unescape operations are performed by means of the org.unbescape.html.HtmlEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
There are simple, preconfigured methods:
final String escaped = HtmlEscape.escapeHtml5(text);
final String unescaped = HtmlEscape.unescapeHtml(escaped);
And also those that allow a more fine-grained configuration of the escape operation:
final String result =
HtmlEscape.escapeHtml(
text,
HtmlEscapeType.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA,
HtmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT);
XML escape and unescape operations are performed by means of the org.unbescape.xml.XmlEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
There are simple, preconfigured methods:
final String escaped = XmlEscape.escapeXml11(text);
final String unescaped = XmlEscape.unescapeXml(escaped);
And also those that allow a more fine-grained configuration of the escape operation:
final String result =
XmlEscape.escapeXml11(
text,
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_DECIMAL,
XmlEscapeLevel.LEVEL_3_ALL_NON_ALPHANUMERIC);
JavaScript escape and unescape operations are performed by means of the org.unbescape.javascript.JavaScriptEscape
class. This class defines a series of static methods that perform the desired operations
(see the class javadoc for more info).
There are simple, preconfigured methods:
final String escaped = JavaScriptEscape.escapeJavaScript(text);
final String unescaped = JavaScriptEscape.unescapeJavaScript(escaped);
And also those that allow a more fine-grained configuration of the escape operation:
final String result =
JavaScriptEscape.escapeJavaScript(
text,
JavaScriptEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_XHEXA_AND_UHEXA,
JavaScriptEscapeLevel.LEVEL_1_BASIC_ESCAPE_SET);
JSON escape and unescape operations are performed by means of the org.unbescape.json.JsonEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
There are simple, preconfigured methods:
final String escaped = JsonEscape.escapeJson(text);
final String unescaped = JsonEscape.unescapeJson(escaped);
And also those that allow a more fine-grained configuration of the escape operation:
final String result =
JsonEscape.escapeJson(
text,
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO__UHEXA,
JsonEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
URI/URL escape and unescape operations are performed by means of the org.unbescape.uri.UriEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
The methods for this type of escape/unescape operations are very simple:
final String escapedPath = UriEscape.escapeUriPath(text);
final String escapedPathSegment = UriEscape.escapeUriPathSegment(text);
final String escapedQueryParam = UriEscape.escapeUriQueryParam(text);
final String escapedFragmentId = UriEscape.escapeUriFragmentId(text);
final String unescapedPath = UriEscape.unescapeUriPath(text);
final String unescapedPathSegment = UriEscape.unescapeUriPathSegment(text);
final String unescapedQueryParam = UriEscape.unescapeUriQueryParam(text);
final String unescapedFragmentId = UriEscape.unescapeUriFragmentId(text);
CSS escape and unescape operations are performed by means of the org.unbescape.css.CssEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
Unbescape includes support for escaping both CSS identifiers and CSS strings (the former type apply
more strict syntax rules).
There are simple, preconfigured methods:
final String escapedIdentifier = CssEscape.escapeCssIdentifier(text);
final String escapedString = CssEscape.escapeCssString(text);
final String unescaped = CssEscape.unescapeCss(escapedIdentifierOrString);
And also those that allow a more fine-grained configuration of the escape operation:
final String identifierResult =
CssEscape.escapeCssIdentifier(
identifierText,
CssIdentifierEscapeType.BACKSLASH_ESCAPES_DEFAULT_TO_SIX_DIGIT_HEXA,
CssIdentifierEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
final String stringResult =
CssEscape.escapeCssString(
stringText,
CssStringEscapeType.BACKSLASH_ESCAPES_DEFAULT_TO_COMPACT_HEXA,
CssStringEscapeLevel.LEVEL_1_BASIC_ESCAPE_SET);
CSV (Comma-Separated Values) escape and unescape operations are performed by means of the org.unbescape.csv.CsvEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
The methods for this type of escape/unescape operations are very simple:
final String escaped = CsvEscape.escapeCsv(text);
final String unescaped = CsvEscape.unescapeCsv(escaped);
Java escape and unescape operations are performed by means of the org.unbescape.java.JavaEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
There are simple, preconfigured methods:
final String escaped = JavaEscape.escapeJava(text);
final String unescaped = JavaEscape.unescapeJava(escaped);
And also those that allow a more fine-grained configuration of the escape operation:
final String result =
JavaEscape.escapeJava(
text,
JavaEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
.properties
File Escape/UnescapeJava .properties
escape and unescape operations are performed by means of the org.unbescape.properties.PropertiesEscape
class. This class
defines a series of static methods that perform the desired operations (see the class javadoc for more info).
Unbescape includes support for escaping both properties keys and properties values (keys require additional
escaping of
, :
and =
).
There are simple, preconfigured methods:
final String escapedKey = PropertiesEscape.escapePropertiesKey(text);
final String escapedString = PropertiesEscape.escapePropertiesValue(text);
final String unescaped = PropertiesEscape.unescapeProperties(escapedKeyOrValue);
And also those that allow a more fine-grained configuration of the escape operation:
final String identifierResult =
PropertiesEscape.escapePropertiesKey(
keyText, PropertiesKeyEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
final String stringResult =
PropertiesEscape.escapePropertiesValue(
valueText, PropertiesValueEscapeLevel.LEVEL_1_BASIC_ESCAPE_SET);