zoukankan      html  css  js  c++  java
  • java对含有中文的字符串进行Unicode编码

    public class MyUtil {
        public static void main(String[] args) throws Exception {
            String s = "a中aabb";
            String url = setUrlForChn(s);
            System.out.println(url);
        }
        
        /**
         * 对含有中文的字符串进行Unicode编码
         * ue400 u9fa5 Unicode表中的汉字的头和尾
         */
        public static String setUrlForChn(String url) throws Exception{
            String regEx = "[u4e00-u9fa5]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(url);
            StringBuffer sb = new StringBuffer();
            while(m.find()){
                m.appendReplacement(sb, URLEncoder.encode(m.group(), "UTF-8"));
            }
            m.appendTail(sb);
            return sb.toString();
        }
    }

    打印:

    a%E4%B8%ADaabb

  • 相关阅读:
    HttpClient
    充值保存
    button 样式
    创建窗口
    第十一次作业
    第十次作业
    第九次作业
    第八次作业
    第七次作业
    第六次作业
  • 原文地址:https://www.cnblogs.com/tenWood/p/8563291.html
Copyright © 2011-2022 走看看