开发者

Replacing Unicode character codes with characters in String in Java

I have a Java String like this: "peque\u00f1o". Note that it has an embedded Unicode character: '\u00f1'.

Is there a method in Java that will replace these Unicode character sequences with the actual characters? That is, a method that would return "pequeño" if you gave it "p开发者_StackOverfloweque\u00f1o" as input?

Note that I have a string that has 12 chars (those that we see, that happen to be in the ASCII range).


Actually the string is "pequeño".

String s = "peque\u00f1o";
System.out.println(s.length());
System.out.println(s);

yields

7
pequeño

i.e. seven chars and the correct representation on System.out.


I remember giving the same response last week, use org.apache.commons.lang.StringEscapeUtils.


If you have the appropriate fonts, a println or setting the string in a JLabel or JTextArea should do the trick. The escaping is only for the compiler.

If you plan to copy-paste the readable strings in source, remember to also choose a suitable file encoding like UTF8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜