欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA 统计字符串中中文,英文,数字,空格,特殊字符的个数

程序员文章站 2024-02-15 18:21:46
引言       可以根据各种字符在unicode字符编码表中的区间来进行判断,如数字为'0'~'9'之间,英文字母为'...

引言

      可以根据各种字符在unicode字符编码表中的区间来进行判断,如数字为'0'~'9'之间,英文字母为'a'~'z'或'a'~'z'等,java判断一个字符串是否有中文是利用unicode编码来判断,因为中文的编码区间为:0x4e00--0x9fbb, 但通用区间来判断中文也不非常精确,因为有些中文的标点符号利用区间判断会得到错误的结果。所以通过character.unicodeblock来进行判断。代码如下:

package cn.csrc.base.count;
public class countcharacter {
  public static void main(string[] args) {
    string str ="我爱你abcd123中国 #!";
    countcharacter countcharacter = new countcharacter();
    countcharacter.count(str);
  }
  /**中文字符 */
  private int chcharacter = 0;
  /**英文字符 */
  private int encharacter = 0;
  /**空格 */
  private int spacecharacter = 0;
  /**数字 */
  private int numbercharacter = 0;
  /**其他字符 */
  private int othercharacter = 0;
  //记录中文字符
   private stringbuilder sb1=new stringbuilder();
  //记录英文字符
   private stringbuilder sb2=new stringbuilder();
  //记录数字
   private stringbuilder sb3=new stringbuilder();
  //记录特殊字符
   private stringbuilder sb4=new stringbuilder();
  /***
  * 统计字符串中中文,英文,数字,空格等字符个数
   * @param str 需要统计的字符串
   */
  public void count(string str) {
    if(str.equals("") || str==null){
      system.out.println("字符串为空");
      return;
    }
    for (int i = 0; i < str.length(); i++) {
      char tmp = str.charat(i);
      if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'a' && tmp <= 'z')) {
        encharacter ++;
        sb2.append(tmp+" ");
      } else if ((tmp >= '0') && (tmp <= '9')) {
        numbercharacter ++;
        sb3.append(tmp +" ");
      } else if (tmp ==' ') {
        spacecharacter ++;
      } else if (ischinese(tmp)) {
        chcharacter ++;
        sb1.append(tmp+" ");
      } else {
        othercharacter ++;
        sb4.append(tmp +" ");
      }
    }
      system.out.println("字符串:" + str + " \r\n");
      system.out.println("中文字符有:" + chcharacter +"个 ("+sb1.tostring()+")");
      system.out.println("英文字符有:" + encharacter +"个 ("+sb2.tostring()+")");
      system.out.println("数字有:" + numbercharacter+"个 ("+sb3.tostring()+")");
      system.out.println("空格有:" + spacecharacter+"个");
      system.out.println("其他字符有:" + othercharacter+"个 ("+sb4.tostring()+")");
    }
    /***
    * 判断字符是否为中文
     * @param ch 需要判断的字符
     * @return 中文返回true,非中文返回false
    */
    private boolean ischinese(char ch) {
      //获取此字符的unicodeblock
      character.unicodeblock ub = character.unicodeblock.of(ch);
      // general_punctuation 判断中文的“号 
       // cjk_symbols_and_punctuation 判断中文的。号 
       if (ub == character.unicodeblock.cjk_unified_ideographs || ub == character.unicodeblock.cjk_compatibility_ideographs 
       || ub == character.unicodeblock.cjk_unified_ideographs_extension_a || ub == character.unicodeblock.cjk_unified_ideog  raphs_extension_b 
     || ub == character.unicodeblock.cjk_symbols_and_punctuation || ub == character.unicodeblock.halfwidth_and_fullwidth_forms 
 || ub == character.unicodeblock.general_punctuation) {
      system.out.println(ch + " 是中文");
      //sb1.append(ch+" ");
      return true;
    }
    return false;
  
   }
}

  结果如下:

     JAVA 统计字符串中中文,英文,数字,空格,特殊字符的个数

总结

以上所述是小编给大家介绍的java 统计字符串中中文,英文,数字,空格,特殊字符的个数,希望对大家有所帮助