简体   繁体   中英

Java doesn't decode passed string (with unicode)

I am creating string (by concatanation of input string and some predefined text), save it into database and pass it to the following method:

public String decodeUTF(String inputString) {
    byte[] bytes =  StringUtils.getBytesUtf8(inputString);
    return StringUtils.newStringUtf8(bytes);
}

When i call (and send result to front(android)) :

decodeUTF("Emoji example: \uD83D\uDE04");

it works (shows smilies) .

When i call

decodeUTF(sameStringFromDb);

it passes whole string whithout conversion.

In Java source code, \? is an escape code : The compiler replaces it with one code unit .

If you see \? in your database, it's not an escape code, it's the sequence of six individual characters '\\' 'u' 'D' '8' '3' 'D'.

What's the right way to fix this and make sure you get the same output anyway?

One thing you must ask is why did the text "\?" get to the database in the first place. Text stored in a database should not be mangled in this way. It sounds like there is a bug at the data entry.

If there's no way to fix the data entry, and you want to replace the text "\?" with a single character just like the Java compiler would, that has already been covered in other questions, see for example Convert escaped Unicode character back to actual character

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