I have the following line of text (see in code as well:
What I'm trying to do do is escape that emoticon (phone icon) as two \u chars then back to its original phone icon? The first method below works fine but I essentially want to escape by a range so that I can escape any chars like this. I don't know how this is possible using the first method below.
How can I achieve this range based escape using the UnicodeEscaper as the same output as StringEscapeUtils (i.e. escape to two \uxx \uxx then unescape back to phone icon)?
import org.apache.commons.lang3.text.translate.UnicodeEscaper; import org.apache.commons.lang3.text.translate.UnicodeUnescaper; String text = "Unicode surrogate here-> 📱<--here"; // escape the entire string...not what I want because there could // be \n \r or any other escape chars that I want left in tact (i just want a range) String text2 = org.apache.commons.lang.StringEscapeUtils.escapeJava(text); System.out.println(text2); // "Unicode surrogate here-> \uD83D\uDCF1<--here" // unescape it back to the phone emoticon text2 = org.apache.commons.lang.StringEscapeUtils.unescapeJava(text); System.out.println(text2); // "Unicode surrogate here-> 📱<--here" // How do I do the same as above but but looking for a range of chars to escape (i.e. any unicode surrogate) // , which is what i want and not to escape the entire string text2 = UnicodeEscaper.between(0x10000, 0x10FFFF).translate(text); System.out.println(text2); // "Unicode surrogate here-> \u1F4F1<--here" // unescape .... (need the phone emoticon here) text2 = (new UnicodeUnescaper().translate(text2)); System.out.println(text2);// "Unicode surrogate here-> 1<--here"
UnicodeEscaper?