简体   繁体   中英

Print string literal unicode as the actual character

In my Java application I have been passed in a string that looks like this:

"\¥123"

When printing that string into the console, I get the same string as the output (as expected).

However, I want to print that out by having the unicode converted into the actual yen symbol (\¥ -> yen symbol) - how would I go about doing this?

ie so it looks like this: "[yen symbol]123"

I wrote a little program:

public static void main(String[] args) {
    System.out.println("\u00a5123");
}

It's output:

¥123

ie it output exactly what you stated in your post. I am not sure there is not something else going on. What version of Java are you using?

edit:

In response to your clarification, there are a couple of different techniques. The most straightforward is to look for a "\\u\u0026quot; followed by 4 hex-code characters, extract that piece and replace with a unicode version with the hexcode (using the Character class). This of course assumes the string will not have a \\u in front of it.

I am not aware of any particular system to parse the String as though it was an encoded Java String.

As has been mentioned before, these strings will have to be parsed to get the desired result.

  1. Tokenize the string by using \\u as separator. For example: \接\受 => { "63A5", "53D7" }

  2. Process these strings as follows:

     String hex = "63A5"; int intValue = Integer.parseInt(hex, 16); System.out.println((char)intValue); 

You're probably going to have to write a parse for these, unless you can find one in a third party library. There is nothing in the JDK to parse these for you, I know because I fairly recently had an idea to use these kind of escapes as a way to smuggle unicode through a Latin-1-only database. (I ended up doing something else btw)

I will tell you that java.util.Properties escapes and unescapes Unicode characters in this manner when reading and writing files (since the files have to be ASCII). The methods it uses for this are private, so you can't call them, but you could use the JDK source code to inspire your solution.

Could replace the above with this:

System.out.println((char)0x63A5);

Here is the code to print all of the box building unicode characters.

public static void printBox()
{
    for (int i=0x2500;i<=0x257F;i++)
    {
        System.out.printf("0x%x : %c\n",i,(char)i);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM