3

My string is:

ĐT70, Châu Linh, Tùng Ảnh, Đức Thọ, Hà Tĩnh, Vietnam.

While passing this text in a url query string, it is coming as:

ĐT70%2C+Châu+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Hà+Tĩnh%2C+Vietnam.

But I want it in this format:

\U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam.

How could I get that string in this format? Please answer.

Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
abhishek
  • 88
  • 1
  • 6
  • 3
    Possible duplicate of [URL encoding in Android](http://stackoverflow.com/questions/3286067/url-encoding-in-android) – xerotolerant Jan 13 '17 at 14:05
  • @xerotolerant...In URL encoding in Android ...after encoding it is coming as this -ĐT70%2C+Châu+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Hà+Tĩnh%2C+Vietnam..while i want in this format - \U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam. – abhishek Jan 13 '17 at 14:16
  • What is coming seems to be correct to me. The format that you are asking for does not seem to be correct. Could you explain why you're trying to do this? – xerotolerant Jan 13 '17 at 14:25
  • Becuse for ios that string is by default convertng into \U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam format. for android it doesn't...and one more thing while passing ĐT70%2C+Châu+Linh%2C+Tùng+Ảnh%2C+Đức+Thọ%2C+Hà+Tĩnh%2C+Vietnam.in url ,i am not getting correct address in response. – abhishek Jan 13 '17 at 14:30

1 Answers1

3

After struggling finally i found solution.

 //String that you want to convert
  String mystring = "133 Phùng Hưng, Cửa Đông, Hoàn Kiếm, Hà Nội, Vietnam";

    //unicodeString is the expected output in unicode 
    String unicodeString = getUnicodeString(escapeUnicodeText(mystring));

   //i want to make small u into Capital from unicode String
  String resultUnicode = unicodeString.replace("\\u", "\\U");

    try {
        String text = URLEncoder.encode(mystring, "UTF-8");
        Log.v("currentDateTimeString", "text String:" + text);
    } catch (Exception e) {
        e.printStackTrace();
    }

Create method escapeUnicodeText to get your Unicode Text into unicode fromat for example if you pass input string ♫ é in escapeUnicodeText() you will get in unicode output like \u266b \u00e9

public String escapeUnicodeText(String input) {

    StringBuilder b = new StringBuilder(input.length());

    java.util.Formatter f = new java.util.Formatter(b);

    for (char c : input.toCharArray()) {
        if (c < 128) {
            b.append(c);
        } else {
            f.format("\\u%04x", (int) c);
        }
    }

    return b.toString();
}

Pass output of escapeUnicodeText into getUnicodeString() you will get your expected result like \U0111\U01b0\U1eddng \U0110\U1eadp, Th\U00f4ng T\U1ef1, T\U00f9ng \U1ea2nh, \U0110\U1ee9c Th\U1ecd, H\U00e0 T\U0129nh, Vietnam.

 public String getUnicodeString(String myString) {
    String text = "";
    try {

         byte[] utf8Bytes = myString.getBytes("UTF8");
        text = new String(utf8Bytes, "UTF8");

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return text;
}
sunil
  • 796
  • 1
  • 8
  • 28