2

How can I escape any Unicode in android OnTextChanged event?? Like "price" to "\u0070\u0072\u0069\u0063\u0065". I tried it but I cant find any solution. This is my 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);

           }
          });

and this is my CharSequence

 private String priceshow (CharSequence s) {
            String priceshowstring= "";
            priceshowstring = s.toString().replace("","");
            System.out.println(priceshowstring);
            return priceshowstring;
            }
T-Heron
  • 5,385
  • 7
  • 26
  • 52
user1992
  • 176
  • 10
  • Why would you need to encode ASCII characters ? – Abkarino Apr 15 '17 at 11:52
  • @Abkarino, i create new app.bet when i add any symbol or some indian language .then i showing only boxex – user1992 Apr 15 '17 at 12:13
  • The example is misleading as you are only using ASCII text in the example. Can you please tell me the source of the text you use? Is it hardcoded or from external source ? – Abkarino Apr 15 '17 at 12:52

1 Answers1

2

To convert character to its Unicode representation you can use

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

Use StringBuilder to convert whole 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();
Biggemot
  • 228
  • 2
  • 6