在 Android OnTextChanged 事件中转义 Unicode:将 "price:10rs" 改为“\u0070\u0072\u0069\u0063\u0065\u003A\u0031\u0030\u0072\u0073”

Escape Unicode in Android OnTextChanged event :Like "price:10rs" to "\u0070\u0072\u0069\u0063\u0065\u003A\u0031\u0030\u0072\u0073"

如何在 android OnTextChanged 事件中转义任何 Unicode?喜欢 "price" 到“\u0070\u0072\u0069\u0063\u0065”。我试过了,但找不到任何解决方案。这是我的 textwatcher

  price.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {
           }

           public void beforeTextChanged(CharSequence s, int start, 
             int count, int after) {
           }

           @SuppressLint("NewApi")
        public void onTextChanged(CharSequence s, int start, 
             int before, int count) {

          MainActivity.this.priceshow(s);

           }
          });

这是我的 CharSequence

 private String priceshow (CharSequence s) {
            String priceshowstring= "";
            priceshowstring = s.toString().replace("","");
            System.out.println(priceshowstring);
            return priceshowstring;
            }

要将字符转换为其 Unicode 表示,您可以使用

String.format("\u%04x", (int) character);

使用 StringBuilder 转换整个 CharSequence

StringBuilder builder = new StringBuilder();
for (int i = 0, length = charSequence.length(); i < length; i++) {
    builder.append(String.format("\u%04x", (int) charSequence.charAt(i)));
}
String result = builder.toString();