java - Replace Unicode escapes with the corresponding character -


i'm trying convert code points, such \u00fc, character represents.

import javax.swing.joptionpane;  public class test {     public static void main(string[] args) {         string in = joptionpane.showinputdialog("write in here");         system.out.println("input: " + in);         // before line         string out = in;         system.out.print("and now: " + out);     } } 

an example explain mean:

first console line: input: hall\u00f6

second console line: and now: hallö

edit: because didn't work multiple unicodes in trombone willy's answer, here code fixed:

public static string unescapeunicode(string s) {     stringbuilder r = new stringbuilder();     (int = 0; < s.length(); i++) {         if (s.length() >= + 6 && s.substring(i, + 2).equals("\\u")) {             r.append(character.tochars(integer.parseint(s.substring(i + 2, + 6), 16)));             += 5;         } else {             r.append(s.charat(i));         }     }     return r.tostring(); } 

joao's answer simplest, function can when don't want have download apache jar, whether space reasons, portability reasons, or don't want mess licenses or other apache cruft. also, since doesn't have functionality, think should faster. here is:

public static string unescapeunicode(string s) {     stringbuilder sb = new stringbuilder();      int oldindex = 0;      (int = 0; + 2 < s.length(); i++) {         if (s.substring(i, + 2).equals("\\u")) {             sb.append(s.substring(oldindex, i));             int codepoint = integer.parseint(s.substring(i + 2, + 6), 16);             sb.append(character.tochars(codepoint));              += 5;             oldindex = i;         }     }      sb.append(s.substring(oldindex + 1, s.length()));      return sb.tostring(); } 

i hope helps! (you don't have give me credit this, give public domain)


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -