2

I have to two strings : 1. bookkeeping forms \\u0026 templates 2. hodgkin\u0027s disease

I want to replace \\u0026 with & and \u0027 with '. One of the way is replace their occurrence with corresponding symbol but is there a generic way to deal with this problem ?

1 Answers1

3

My recommendation is Apache StringScapeUtils specifically the unescapeJava method.

Site: https://commons.apache.org/proper/commons-lang/

Maven repo: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

enter image description here

That method converts unicode to readable text, for example:

String stringWithUnicode; //For example "tendr\\u00e1"
StringScapeUtils.unescapeJava(stringWithUnicode); //This call returns: Tendrá
Ele
  • 33,468
  • 7
  • 37
  • 75
  • In your example, there should be two `\ `s in the string literal, otherwise you may as well use the literal by itself. – bcsb1001 Dec 27 '17 at 16:40
  • Your code example should be using a double backslash `StringScapeUtils.unescapeJava("\\u00e1");` , otherwise the string literal already contains the single character `á`, unescaped by the Java compiler. – Ralf Kleberhoff Dec 27 '17 at 16:41