• Java 判断中文字符


      1 package com.coder4j.main;
      2 
      3 import java.util.regex.Pattern;
      4 
      5 /**
      6  * Java 判断中文字符
      7  * 
      8  * @author Chinaxiang
      9  * @date 2015-08-11
     10  *
     11  */
     12 public class CheckChinese {
     13 
     14     public static void main(String[] args) {
     15         // 纯英文
     16         String s1 = "Hello,Tom.!@#$%^&*()_+-={}|[];':"?";
     17         // 纯中文(不含中文标点)
     18         String s2 = "你好中国";
     19         // 纯中文(含中文标点)
     20         String s3 = "你好,中国。《》:“”‘’;()【】!¥、";
     21         // 韩文
     22         String s4 = "한국어난";
     23         // 日文
     24         String s5 = "ぎじゅつ";
     25         // 特殊字符
     26         String s6 = "��";
     27         String s7 = "╃";
     28         String s8 = "╂";
     29         // 繁体中文
     30         String s9 = "蒼老師";
     31 
     32         // 1 使用字符范围判断
     33         System.out.println("s1是否包含中文:" + hasChineseByRange(s1));// false
     34         System.out.println("s2是否包含中文:" + hasChineseByRange(s2));// true
     35         System.out.println("s3是否包含中文:" + hasChineseByRange(s3));// true
     36         System.out.println("s4是否包含中文:" + hasChineseByRange(s4));// false
     37         System.out.println("s5是否包含中文:" + hasChineseByRange(s5));// false
     38         System.out.println("s6是否包含中文:" + hasChineseByRange(s6));// false
     39         System.out.println("s7是否包含中文:" + hasChineseByRange(s7));// false
     40         System.out.println("s8是否包含中文:" + hasChineseByRange(s8));// false
     41         System.out.println("s9是否包含中文:" + hasChineseByRange(s9));// true
     42         System.out.println("-------分割线-------");
     43         System.out.println("s1是否全是中文:" + isChineseByRange(s1));// false
     44         System.out.println("s2是否全是中文:" + isChineseByRange(s2));// true
     45         System.out.println("s3是否全是中文:" + isChineseByRange(s3));// false 中文标点不在范围内
     46         System.out.println("s4是否全是中文:" + isChineseByRange(s4));// false
     47         System.out.println("s5是否全是中文:" + isChineseByRange(s5));// false
     48         System.out.println("s6是否全是中文:" + isChineseByRange(s6));// false
     49         System.out.println("s7是否全是中文:" + isChineseByRange(s7));// false
     50         System.out.println("s8是否全是中文:" + isChineseByRange(s8));// false
     51         System.out.println("s9是否全是中文:" + isChineseByRange(s9));// true
     52         System.out.println("-------分割线-------");
     53         // 2 使用字符范围正则判断(结果同1)
     54         System.out.println("s1是否包含中文:" + hasChineseByReg(s1));// false
     55         System.out.println("s2是否包含中文:" + hasChineseByReg(s2));// true
     56         System.out.println("s3是否包含中文:" + hasChineseByReg(s3));// true
     57         System.out.println("s4是否包含中文:" + hasChineseByReg(s4));// false
     58         System.out.println("s5是否包含中文:" + hasChineseByReg(s5));// false
     59         System.out.println("s6是否包含中文:" + hasChineseByReg(s6));// false
     60         System.out.println("s7是否包含中文:" + hasChineseByReg(s7));// false
     61         System.out.println("s8是否包含中文:" + hasChineseByReg(s8));// false
     62         System.out.println("s9是否包含中文:" + hasChineseByReg(s9));// true
     63         System.out.println("-------分割线-------");
     64         System.out.println("s1是否全是中文:" + isChineseByReg(s1));// false
     65         System.out.println("s2是否全是中文:" + isChineseByReg(s2));// true
     66         System.out.println("s3是否全是中文:" + isChineseByReg(s3));// false 中文标点不在范围内
     67         System.out.println("s4是否全是中文:" + isChineseByReg(s4));// false
     68         System.out.println("s5是否全是中文:" + isChineseByReg(s5));// false
     69         System.out.println("s6是否全是中文:" + isChineseByReg(s6));// false
     70         System.out.println("s7是否全是中文:" + isChineseByReg(s7));// false
     71         System.out.println("s8是否全是中文:" + isChineseByReg(s8));// false
     72         System.out.println("s9是否全是中文:" + isChineseByReg(s9));// true
     73         System.out.println("-------分割线-------");
     74         // 3 使用CJK字符集判断
     75         System.out.println("s1是否包含中文:" + hasChinese(s1));// false
     76         System.out.println("s2是否包含中文:" + hasChinese(s2));// true
     77         System.out.println("s3是否包含中文:" + hasChinese(s3));// true
     78         System.out.println("s4是否包含中文:" + hasChinese(s4));// false
     79         System.out.println("s5是否包含中文:" + hasChinese(s5));// false
     80         System.out.println("s6是否包含中文:" + hasChinese(s6));// false
     81         System.out.println("s7是否包含中文:" + hasChinese(s7));// false
     82         System.out.println("s8是否包含中文:" + hasChinese(s8));// false
     83         System.out.println("s9是否包含中文:" + hasChinese(s9));// true
     84         System.out.println("-------分割线-------");
     85         System.out.println("s1是否全是中文:" + isChinese(s1));// false
     86         System.out.println("s2是否全是中文:" + isChinese(s2));// true
     87         System.out.println("s3是否全是中文:" + isChinese(s3));// true 中文标点也被包含进来
     88         System.out.println("s4是否全是中文:" + isChinese(s4));// false
     89         System.out.println("s5是否全是中文:" + isChinese(s5));// false
     90         System.out.println("s6是否全是中文:" + isChinese(s6));// false
     91         System.out.println("s7是否全是中文:" + isChinese(s7));// false
     92         System.out.println("s8是否全是中文:" + isChinese(s8));// false
     93         System.out.println("s9是否全是中文:" + isChinese(s9));// true
     94 
     95     }
     96 
     97     /**
     98      * 是否包含中文字符<br>
     99      * 包含中文标点符号<br>
    100      * 
    101      * @param str
    102      * @return
    103      */
    104     public static boolean hasChinese(String str) {
    105         if (str == null) {
    106             return false;
    107         }
    108         char[] ch = str.toCharArray();
    109         for (char c : ch) {
    110             if (isChinese(c)) {
    111                 return true;
    112             }
    113         }
    114         return false;
    115     }
    116 
    117     /**
    118      * 是否全是中文字符<br>
    119      * 包含中文标点符号<br>
    120      * 
    121      * @param str
    122      * @return
    123      */
    124     public static boolean isChinese(String str) {
    125         if (str == null) {
    126             return false;
    127         }
    128         char[] ch = str.toCharArray();
    129         for (char c : ch) {
    130             if (!isChinese(c)) {
    131                 return false;
    132             }
    133         }
    134         return true;
    135     }
    136 
    137     /**
    138      * 是否是中文字符<br>
    139      * 包含中文标点符号<br>
    140      * 
    141      * @param c
    142      * @return
    143      */
    144     private static boolean isChinese(char c) {
    145         Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    146         if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
    147             return true;
    148         } else if (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) {
    149             return true;
    150         } else if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
    151             return true;
    152         } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
    153             return true;
    154         } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) {
    155             return true;
    156         } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C) {
    157             return true;
    158         } else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) {
    159             return true;
    160         } else if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
    161             return true;
    162         } else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
    163             return true;
    164         }
    165         return false;
    166     }
    167 
    168     /**
    169      * 是否包含汉字<br>
    170      * 根据汉字编码范围进行判断<br>
    171      * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)<br>
    172      * 
    173      * @param str
    174      * @return
    175      */
    176     public static boolean hasChineseByReg(String str) {
    177         if (str == null) {
    178             return false;
    179         }
    180         Pattern pattern = Pattern.compile("[\u4E00-\u9FBF]+");
    181         return pattern.matcher(str).find();
    182     }
    183 
    184     /**
    185      * 是否全是汉字<br>
    186      * 根据汉字编码范围进行判断<br>
    187      * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)<br>
    188      * 
    189      * @param str
    190      * @return
    191      */
    192     public static boolean isChineseByReg(String str) {
    193         if (str == null) {
    194             return false;
    195         }
    196         Pattern pattern = Pattern.compile("[\u4E00-\u9FBF]+");
    197         return pattern.matcher(str).matches();
    198     }
    199 
    200     /**
    201      * 是否包含汉字<br>
    202      * 根据汉字编码范围进行判断<br>
    203      * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)<br>
    204      * 
    205      * @param str
    206      * @return
    207      */
    208     public static boolean hasChineseByRange(String str) {
    209         if (str == null) {
    210             return false;
    211         }
    212         char[] ch = str.toCharArray();
    213         for (char c : ch) {
    214             if (c >= 0x4E00 && c <= 0x9FBF) {
    215                 return true;
    216             }
    217         }
    218         return false;
    219     }
    220 
    221     /**
    222      * 是否全是汉字<br>
    223      * 根据汉字编码范围进行判断<br>
    224      * CJK统一汉字(不包含中文的,。《》()“‘’”、!¥等符号)<br>
    225      * 
    226      * @param str
    227      * @return
    228      */
    229     public static boolean isChineseByRange(String str) {
    230         if (str == null) {
    231             return false;
    232         }
    233         char[] ch = str.toCharArray();
    234         for (char c : ch) {
    235             if (c < 0x4E00 || c > 0x9FBF) {
    236                 return false;
    237             }
    238         }
    239         return true;
    240     }
    241 
    242 }

    项目偶尔需要对中文字符做一些处理,所以搜集了这个判断中文字符的代码片段,分享给大家。

    直接贴出代码了,里面有详细的注释。

    作者:Chinaxiang
    链接:https://segmentfault.com/a/1190000004408403
    来源:SegmentFault 思否

  • 相关阅读:
    Filter
    Servlet
    Maven(Mac)
    SpringMVC 完美解决PUT请求参数绑定问题(普通表单和文件表单)
    Android Bitmap
    Android ContentProvider
    浅谈数据库事务隔离
    开启Spring Initializr个性化之旅
    java浮点型精度丢失浅析
    爬取糗事百科段子
  • 原文地址:https://www.cnblogs.com/littlebob/p/13218053.html
Copyright © 2020-2023  润新知