package com.yiboshi.science.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EncodingUtil {

    /**
     * 对含有中文字符的字符串进行Unicode编码(包括特殊字符)
     */
    public static String setUrlForChn(String url) {
        String regEx = "[^\\x00-\\xff]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(url);
        StringBuffer sb = new StringBuffer();
        while(m.find()){
            String str = m.group();
            System.out.println(str);
            char[] chars = str.toCharArray();
            m.appendReplacement(sb, "&#" + String.valueOf((int)chars[0]) + ";");
        }
        m.appendTail(sb);
        return sb.toString();
    }
}

