Use:

StringEscapeUtils.escapeHtml("I'm coder")

Escapes the characters in a String using HTML entities.

For example:

"bread" & "butter"

Becomes:

"bread" & "butter".

Answer from Srikanth Puliroju on Stack Overflow
🌐
Apache Commons
commons.apache.org › proper › commons-lang › apidocs › org › apache › commons › lang3 › StringEscapeUtils.html
StringEscapeUtils (Apache Commons Lang 3.21.0-SNAPSHOT API)
For example: "bread" & "butter" => "bread" & "butter". Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities. Note that Unicode characters greater than 0x7f are as of 3.0, no longer escaped.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-2.6 › org › apache › commons › lang › StringEscapeUtils.html
StringEscapeUtils (Commons Lang 2.6 API)
January 10, 2011 - escapeHtml(String), unescapeHt... characters corresponding to the escapes. Supports HTML 4.0 entities. For example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Français>"...
🌐
Apache Commons
commons.apache.org › proper › commons-text › javadocs › api-release › org › apache › commons › text › StringEscapeUtils.html
StringEscapeUtils (Apache Commons Text 1.9 API)
Supports only the HTML 3.0 entities. ... Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities. For example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Fran�ais>"
🌐
Tabnine
tabnine.com › home page › code › java › org.apache.commons.lang.stringescapeutils
org.apache.commons.lang.StringEscapeUtils.escapeHtml java code examples | Tabnine
@NotNull private static String preludeContentLineToHTMLLine(@NotNull String contentLine, @NotNull String workingDirectory) { Matcher explainableMatcher = EXPLAINABLE_PATTERN.matcher(contentLine); String htmlLine; if (explainableMatcher.find()) { String explainable = explainableMatcher.group("explainable"); int before = explainableMatcher.start("explainable"); int after = explainableMatcher.end("explainable"); htmlLine = escapeHtml(contentLine.substring(0, before)) + "<a href=\"" + navigationHref(workingDirectory, explainableMatcher) + "\">" + explainable + "</a>" + escapeHtml(contentLine.substring(after)); } else { htmlLine = escapeHtml(contentLine); } return htmlLine + "<br/>"; } ... public static String convertToHtml(String input) { return new Markdown().convert(StringEscapeUtils.escapeHtml(input)); } }
🌐
Java Tips
javatips.net › api › org.apache.commons.lang.stringescapeutils
Java Examples for org.apache.commons.lang.StringEscapeUtils
Example 5 · @Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); if (values != null) { int length = values.length; String[] escapseValues = new String[length]; for (int i = 0; i < length; i++) { escapseValues[i] = StringEscapeUtils.escapeHtml4(values[i]); } return escapseValues; } return super.getParameterValues(name); } Example 6 ·
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-3.1 › org › apache › commons › lang3 › StringEscapeUtils.html
StringEscapeUtils (Commons Lang 3.1 API) - Apache Commons
July 19, 2011 - For example: "bread" & "butter" => &amp;quot;bread&quot; &amp; &quot;butter&quot;. Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities. Note that Unicode characters greater than 0x7f are as of 3.0, no longer escaped.
🌐
Javased
javased.com › index.php
Java Code Examples of org.apache.commons.lang.StringEscapeUtils
Example 3 · From project nuxeo-opensocial, under directory /nuxeo-opensocial-spaces/src/main/java/org/nuxeo/opensocial/container/server/webcontent/gadgets/html/. Source file: HTMLAdapter.java · 34 · public void feedFrom(HTMLData data) throws ClientException { super.setMetadataFrom(data); doc.setPropertyValue(WC_HTML_HTML_PROPERTY,StringEscapeUtils.escapeHtml(data.getHtml())); doc.setPropertyValue(WC_HTML_HTML_TITLE_PROPERTY,data.getHtmlTitle()); doc.setPropertyValue(WC_HTML_HTML_TEMPLATE_PROPERTY,data.getTemplate()); doc.setPropertyValue("wchtml:htmlpicturelegend",data.getHtmlPictureLegend(
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › string › java escape html – encode special characters
Java Escape HTML - Encode Special Characters
October 10, 2023 - String unEscapedString = "<java>public static void main(String[] args) { ... }</java>"; String escapedHTML = StringEscapeUtils.escapeHtml4(unEscapedString); System.out.println(escapedHTML); //Browser can now parse this and print
Top answer
1 of 4
19

I can think of several possibilities to explain why sometimes a string is not escaped:

  • perhaps the original programmer was confident that at certain places the string had no special characters (however, in my opinion this would be bad programming practice; it costs very little to escape a string as protection against future changes)
  • the string was already escaped at that point in the code. You definitely don't want to escape a string twice; the user will end up seeing the escape sequence instead of the intended text.
  • The string was the actual html itself. You don't want to escape the html; you want the browser to process it!

EDIT - The reason for escaping is that special characters like & and < can end up causing the browser to display something other than what you intended. A bare & is technically an error in the html. Most browsers try to deal intelligently with such errors and will display them correctly in most cases. (This will almost certainly happen in your example text if the string were text in a <div>, for instance.) However, because it is bad markup, some browsers will not work well; assistive technologies (e.g., text-to-speech) may fail; and there may be other problems.

There are several cases that will fail despite the best efforts of the browser to recover from bad markup. If your sample string were an attribute value, escaping the quote marks would be absolutely required. There's no way that a browser is going to correctly handle something like:

<img alt=""bread" & "butter"" ... >

The general rule is that any character that is not markup but might be confused as markup need to be escaped.

Note that there are several contexts in which text can appear within an html document, and they have separate requirements for escaping. The following should be escaped:

  • all characters that have no representation in the character set of the document (unlikely if you are using UTF-8, but that's not always the case)
  • Within attribute values, quote marks (' or ", whichever one matches the delimiters used for the attribute value itself) and the ampersand (&), but not <
  • Within text nodes, only & and <
  • Within href values, characters that need escaping in a url (and sometimes these need to be doubly escaped so they are still escaped after the browser unescapes them once)
  • Within a CDATA block, generally nothing (at the HTML level).

Finally, aside from the hazard of double-escaping, the cost of escaping all text is minimal: a tiny bit of extra processing and a few extra bytes on the network.

2 of 4
3

HTML (nowadays we would better say XML) defines many so called "special" characters, which means that these characters have special meaning for browser in contrast with "normal" characters that just mean themselves. For example, string "Hello, World!" contains only "normal" characters and thus it literally means "Hello, World!" for browser. String "<b>Hello, World!</b>", contains special characters '<', '>' and '/', and for browser it means: typeset string "Hello, World!" in bold instead of just typeset "<b>Hello, World!</b>".

Method escapeHtml (String) probably (I cannot tell for sure because I don't know how it is implemented) converts arbitrary string into HTML code that will instruct browser to literally typeset this string. For example, escapeHtml ("<b>Hello, World!</b>") whill return HTML code that will be interpreted by browser as typeset "<b>Hello, World!</b>" normally instead of typeset string "Hello, World!" in bold. If method escapeHtml (String) is implemented correctly, you should not care how HTML code produced by this method looks like. Just use it where you want to ask browser to typeset some string literally.

🌐
Mkyong
mkyong.com › home › java › how to escape html in java
How to escape HTML in Java - Mkyong.com
January 20, 2020 - package com.mkyong.html; // make ... JavaEscapeHtmlExample { public static void main(String[] args) { String html = "<h1> hello & world</h1>"; String output = StringEscapeUtils.escapeHtml4(html); System.out.println(output); } } Output ...
🌐
GitHub
github.com › apache › commons-text › blob › master › src › main › java › org › apache › commons › text › StringEscapeUtils.java
commons-text/src/main/java/org/apache/commons/text/StringEscapeUtils.java at master · apache/commons-text
public class StringEscapeUtils { · /* ESCAPE TRANSLATORS */ · /** * Convenience wrapper for {@link StringBuilder} providing escape methods. * * <p>Example:</p> * <pre> * new Builder(ESCAPE_HTML4) * .append("&lt;p&gt;") * .escape("This is paragraph 1 and special chars like &amp; get escaped.") * .append("&lt;/p&gt;&lt;p&gt;") * .escape("This is paragraph 2 &amp; more...") * .append("&lt;/p&gt;") * .toString() * </pre> */ public static final class Builder { ·
Author   apache
🌐
Baeldung
baeldung.com › home › java web › unescape html symbols in java
Unescape HTML Symbols in Java | Baeldung
July 15, 2024 - String expectedQuote = "\"Hello\" Baeldung"; String escapedQuote = "&quot;Hello&quot; Baeldung"; Assert.assertEquals(expectedQuote, StringEscapeUtils.unescapeHtml4(escapedQuote)); String escapedStringsWithHtmlSymbol = "&lt;p&gt;&lt;strong&gt;Test ...
🌐
GitHub
github.com › ervandew › formic › blob › main › src › java › org › apache › commons › lang › StringEscapeUtils.java
formic/src/java/org/apache/commons/lang/StringEscapeUtils.java at main · ervandew/formic
escapeHtml(writer, str); return writer.toString(); } catch (IOException e) { //assert false; //should be impossible · e.printStackTrace(); return null; } } · /** * <p>Escapes the characters in a <code>String</code> using HTML entities and writes · * them to a <code>Writer</code>.</p> * * <p> * For example: * </p> ·
Author   ervandew
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Escape HTML Symbols - Java Code Geeks
September 12, 2023 - package com.jcg.example; import ... text.</p>"; // Escape HTML symbols using StringEscapeUtils String escapedHtml = StringEscapeUtils.escapeHtml4(unescapedHtml); // Print the escaped HTML System.out.println("Escaped HTML:"); ...
🌐
Baeldung
baeldung.com › home › java › java string › escape html symbols in java
Escape HTML Symbols in Java | Baeldung
January 8, 2024 - String input = "<div>Escape & test</div>"; String escapedOutput = StringEscapeUtils.escapeHtml4(input);