[英]Escaping unicode surrogate characters?
我有以下文本行(也請參見代碼:
我想做的是將表情符號(電話圖標)轉義為兩個\\ u字符,然后返回其原始電話圖標? 下面的第一個方法可以正常工作,但我本質上是想按一定范圍進行轉義,以便可以轉義任何這樣的字符。 我不知道如何使用下面的第一種方法。
如何使用UnicodeEscaper作為與StringEscapeUtils相同的輸出來實現基於范圍的轉義(即轉義為兩個\\ uxx \\ uxx,然后轉義為電話圖標)?
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"
答案太晚了。 但是我發現你需要
org.apache.commons.lang3.text.translate.JavaUnicodeEscaper
類而不是UnicodeEscaper。
使用它可以打印:
Unicode surrogate here-> \uD83D\uDCF1<--here
並且逃避效果很好。
您的字符串:
"Unicode surrogate here-> \u1F4F1<--here"
不按照您的想法去做。
char
基本上是UTF-16代碼單元,因此為16位。 所以這里發生的是您有\ὁ 1
; 這說明了您的輸出。
我不知道您在這里所說的“轉義”,但是如果這用“ \\ u \\ u”代替了代理對,那么請看一下Character.toChars()
。 它將返回表示一個Unicode代碼點所必需的char
序列,無論它在BMP中(一個char)還是不在BMP中(兩個char)。
對於代碼點U + 1f4f1,它將返回一個具有兩個元素的char數組,該數組分別具有字符0xd83d和0xdcf1。 這就是您想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.