encode Chinese - Java Internationalization

Java examples for Internationalization:Chinese

Description

encode Chinese

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static String encodeChinese(String url) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < url.length(); i++) {
            char ch = url.charAt(i);
            if (isChinese(ch)) {
                try {
                    sb.append(java.net.URLEncoder.encode(ch + "", "UTF8"));
                } catch (UnsupportedEncodingException e) {

                }/*from  w w  w.  j ava2 s.co m*/
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    private static boolean isChinese(char ch) {
        return (ch >= '\u4E00' && ch <= '\u9FA5')
                || (ch >= '\uF900' && ch <= '\uFA2D');
    }
}

Related Tutorials