zoukankan      html  css  js  c++  java
  • java 中文转Unicode 以及 Unicode转中文

    package com.sun;

    public class Snippet {
        public static void main(String[] args) {
            String cn = "你";
            System.out.println(cnToUnicode(cn));
            // 字符串 : u5f00u59cbu4efbu52a1 ,由于 在java里是转义字符,要写出下面这种形式
            String unicode = "\u4f60";
            System.out.println(unicodeToCn(unicode));
        }
        
        private static String unicodeToCn(String unicode) {
            /** 以 u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/
            String[] strs = unicode.split("\\u");
            String returnStr = "";
            // 由于unicode字符串以 u 开头,因此分割出的第一个字符是""。
            for (int i = 1; i < strs.length; i++) {
              returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
            }
            return returnStr;
        }
        
        private static String cnToUnicode(String cn) {
            char[] chars = cn.toCharArray();
            String returnStr = "";
            for (int i = 0; i < chars.length; i++) {
              returnStr += "\u" + Integer.toString(chars[i], 16);
            }
            return returnStr;
        }
    }

    效果:

  • 相关阅读:
    个人冲刺二(2)
    个人冲刺二(1)
    三个和尚观后感
    每日总结
    个人冲刺(10)
    个人冲刺(9)
    个人冲刺(8)
    个人冲刺(7)
    个人冲刺(6)
    下次视频面试前把电脑摄像头擦干净吧
  • 原文地址:https://www.cnblogs.com/jpfss/p/10070334.html
Copyright © 2011-2022 走看看