Java UTF8 Encode utf8urlencode(String text)

Here you can find the source of utf8urlencode(String text)

Description

utfurlencode

License

Open Source License

Declaration

public static String utf8urlencode(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.charset.StandardCharsets;

public class Main {

    public static String utf8urlencode(String text) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {

            char c = text.charAt(i);
            if (c <= 255) {
                result.append(c);/*from w w w.  j a v a2s.  com*/
            } else {

                byte[] b = new byte[0];
                try {
                    b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
                } catch (Exception ignored) {
                }

                for (byte aB : b) {
                    int k = aB;
                    if (k < 0) {
                        k += 256;
                    }
                    result.append("%").append(Integer.toHexString(k).toUpperCase());
                }

            }
        }
        return result.toString();
    }
}

Related

  1. utf8Encode(String str)
  2. utf8Encode(String str)
  3. Utf8Encode(String string)
  4. utf8Encode(String url)
  5. utf8FromString(String sIn)