[Java] Java统计字符串中汉字,英文,数字,特殊符号个数 →→→→→进入此内容的聊天室

来自 , 2020-09-10, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/a891af9b
  1. package wzs.arithmetics;
  2.  
  3. /**
  4.  * 分别统计出其中字符串中汉字,英文字母,数字,其他字符数量
  5.  * @author wWX154783
  6.  *
  7.  */
  8. public class Test_wzs7
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         String str = "a12中国3@b&4语*言3c";
  13.  
  14.         String E1 = "[\u4e00-\u9fa5]";// 中文
  15.         String E2 = "[a-zA-Z]";// 英文
  16.         String E3 = "[0-9]";// 数字
  17.  
  18.         int chineseCount = 0;
  19.         int englishCount = 0;
  20.         int numberCount = 0;
  21.  
  22.         String temp;
  23.         for (int i = 0; i < str.length(); i++)
  24.         {
  25.             temp = String.valueOf(str.charAt(i));
  26.             if (temp.matches(E1))
  27.             {
  28.                 chineseCount++;
  29.             }
  30.             if (temp.matches(E2))
  31.             {
  32.                 englishCount++;
  33.             }
  34.             if (temp.matches(E3))
  35.             {
  36.                 numberCount++;
  37.             }
  38.         }
  39.         System.out.println("汉字数:" + chineseCount);
  40.         System.out.println("英文数:" + englishCount);
  41.         System.out.println("数字数:" + numberCount);
  42.         System.out.println("特殊字符:" + (str.length() - (chineseCount + englishCount + numberCount)));
  43.     }
  44. }
  45.  
  46. //java/6228

回复 "Java统计字符串中汉字,英文,数字,特殊符号个数"

这儿你可以回复上面这条便签

captcha