How to convert special characters using java?

I have lines like:

Avery® Laser & Inkjet Self-Adhesive 

I need to convert them to

 Avery Laser & Inkjet Self-Adhesive. 

those. remove special characters and convert html special characters to regular ones.

+7
java special-characters htmlspecialchars
source share
4 answers
  Avery® Laser & amp;  Inkjet Self-Adhesive 

First use StringEscapeUtils#unescapeHtml4() (or #unescapeXml() , depending on the source format) to cancel & in & . Then use String#replaceAll() with [^\x20-\x7e] to get rid of characters that are not inside the printable ASCII range .

Summarized:

 String clean = StringEscapeUtils.unescapeHtml4(dirty).replaceAll("[^\\x20-\\x7e]", ""); 

.. which produces

  Avery Laser & Inkjet Self-Adhesive 

(without endpoint, as in your example, but which is not in the original;))

However, this is more like a workaround request than a solution request. If you talk in detail about the functional requirement and / or where this line originated, we can provide the right solution. ® it seems that this is caused by incorrect coding for reading a string, and & Looks like caused by using a text parser to read a string instead of a full-featured HTML parser.

+18
source share

You can use the StringEscapeUtils class from the Apache Commons Lang project.

+6
source share

Perhaps you can use something like:

 yourTxt = yourTxt.replaceAll("&", "&"); 

in some project, I did something like:

 public String replaceAcutesHTML(String str) { str = str.replaceAll("á","á"); str = str.replaceAll("é","é"); str = str.replaceAll("í","í"); str = str.replaceAll("ó","ó"); str = str.replaceAll("ú","ú"); str = str.replaceAll("Á","Á"); str = str.replaceAll("É","É"); str = str.replaceAll("Í","Í"); str = str.replaceAll("Ó","Ó"); str = str.replaceAll("Ú","Ú"); str = str.replaceAll("ñ","ñ"); str = str.replaceAll("Ñ","Ñ"); return str; 

}

+1
source share

If you want to emulate that the php function htmlspecialchars_decode uses the php function get_html_translation_table () to unload the table, and then use java code, e.g.

  static Hashtable html_specialchars_table = new Hashtable(); static { html_specialchars_table.put("&lt;","<"); html_specialchars_table.put("&gt;",">"); html_specialchars_table.put("&amp;","&"); } static String htmlspecialchars_decode_ENT_NOQUOTES(String s){ Enumeration en = html_specialchars_table.keys(); while(en.hasMoreElements()){ String key = (String)en.nextElement(); String val = (String)html_specialchars_table.get(key); s = s.replaceAll(key, val); } return s; } 
+1
source share

All Articles