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, \uD83D
is an escape code: The compiler replaces it with one code unit.
If you see \uD83D
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 "\uD83D" 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 "\uD83D" 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