铁马冰河2000

导航

String中文工具类-ChinaUtil

============================================================================String中文工具类:

public class ChinaUtil {
    
    /**  是否含有中文字符
     * @param input
     * @return
     */
    public static boolean hasChinese(String input) {
        if(StringCheckUtil.isEmpty(input)) {
            return false;
        }
        for(char c : input.toCharArray()) {
            if(isChinese(c)) {
                return true;
            }
        }
        return false;
    }
    
    /** 判断是否为中文字符 
     * @param c 
     * @return 
     */  
    public static boolean isChinese(char c) {  
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
            return true;  
        }
        return false;  
    } 
    
    /** 把中文转成Unicode码 
     * @param str 
     * @return 
     */  
    public static String chinaToUnicode(String str) {  
        String result = "";  
        for (int i = 0; i < str.length(); i++) {  
            int chr1 = (char) str.charAt(i);  
            if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)  
                result += "\\u" + Integer.toHexString(chr1);  
            } else {  
                result += str.charAt(i);  
            }  
        }  
        return result;  
    } 
    
    /**Unicode转中文  
     * @param unicode
     * @return
     */
    public static String decodeUnicode(final String unicode) {     
         StringBuffer string = new StringBuffer();  
         String[] hex = unicode.split("\\\\u");  
         for (int i = 0; i < hex.length; i++) {  
             try {  
                 // 汉字范围 \u4e00-\u9fa5 (中文)  
                 if(hex[i].length()>=4){//取前四个,判断是否是汉字  
                     String chinese = hex[i].substring(0, 4);  
                     try {  
                         int chr = Integer.parseInt(chinese, 16);  
                         boolean isChinese = isChinese((char) chr);  
                         //转化成功,判断是否在  汉字范围内  
                         if (isChinese){//在汉字范围内  
                             // 追加成string  
                             string.append((char) chr);  
                             //并且追加  后面的字符  
                             String behindString = hex[i].substring(4);  
                             string.append(behindString);  
                         }else {  
                             string.append(hex[i]);  
                         }  
                     } catch (NumberFormatException e1) {  
                         string.append(hex[i]);  
                     }  
                       
                 }else{  
                     string.append(hex[i]);  
                 }  
             } catch (NumberFormatException e) {  
                 string.append(hex[i]);  
             }  
         }  
         return string.toString();  
    }  
}

============================================================================String中文工具测试类:

    /**
     * 中文转换
     */
    @Test
    public void test_china() {
        System.out.println(ChinaUtil.isChinese('陶'));
        System.out.println(ChinaUtil.hasChinese("tao渊明"));
        String chinaToUnicode = ChinaUtil.chinaToUnicode("陶渊明");
        System.out.println(chinaToUnicode);
        String decodeUnicode = ChinaUtil.decodeUnicode(chinaToUnicode);
        System.out.println(decodeUnicode);
    }

 

posted on 2022-02-03 17:34  铁马冰河2000  阅读(72)  评论(0编辑  收藏  举报