java - StringEscapeUtils: How to unescape a string except emoji? -


i using stringescapeutils#escapejava escape strings. character: "é" (latin small letter e acute) transformed "\u00e9" , "😅" (smiling face open mouth , cold sweat) transformed "\ud83d\ude05". if want unescape them, revert original form. want unescape "\u00e9" "é" , keep "\ud83d\ude05" is. should emojis not escaped alphabets are?

it might easier "fully unescape" string, , re-escape emoji. can detecting surrogate pairs of characters, using character.islowsurrogate , character.ishighsurrogate.

for example:

stringbuilder sb = new stringbuilder(str.length()); (int = 0; < str.length(); ++i) {   char c = str.charat(i);   if (character.ishighsurrogate(c) || character.islowsurrogate(c)) {     // append escaped character.     sb.append("\\u");     sb.append(string.format("%04x", (int) c));   } else {     // append character as-is.     sb.append(c);   } } string partlyescaped = sb.tostring(); 

ideone demo


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -