I want to escape $ symbol along with other characters like '<', '>' etc. So just wanted to know if StringEscapeUtils supported this. And if not, how can I escape it?
Asked
Active
Viewed 3,892 times
0
-
3If only there was a way to find out if it gets replaced... – f1sh Dec 01 '17 at 08:32
-
Some JavaScript heavily uses $. But one can do `html.replace("$", "%24")` – Joop Eggen Dec 01 '17 at 08:44
-
@JoopEggen This link shows "$" for $ - http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php It this somthing different? – mmk Dec 01 '17 at 09:22
-
@mmk I was wrong, `$` is better for HTML text. – Joop Eggen Dec 01 '17 at 10:10
2 Answers
1
Looks like no. You can try it with this code:
StringEscapeUtils.escapeHtml("<b>Hello, World!</b>$")
Outputs
<b>Hello, World!</b>$

Nick
- 823
- 2
- 10
- 22
1
I tested doing the following from commons-text 1.1 after commons-lang 3 deprecated StringEscapeUtils
:
public static void main(String[] args)
{
System.out.println(StringEscapeUtils.escapeHtml4("$ % > < = #"));
}
Output $ % > < = #
It does not escape "$" out of the box, but the new Utils in commons-text enable users to extend it. Read this article for examples. Here is a small one, but the article shows more advanced possibilities:
Map<CharSequence, CharSequence> added = new HashMap<>();
added.put("$", "foo");
System.out.println(StringEscapeUtils.ESCAPE_HTML4
.with(new LookupTranslator(added))
.translate("$ % > < = #"));
Output foo % > < = #

technocrat
- 3,513
- 5
- 25
- 39

Malte Hartwig
- 4,477
- 2
- 14
- 30
-
If I want to do it manually (using replaceAll) what should be the value, any idea? – mmk Dec 01 '17 at 09:26
-
Whether you do it manually or with the escape utils, the code you found and mentioned in your reply to Joop should be the one. The percent encoded one is for URLs. – Malte Hartwig Dec 01 '17 at 09:58