获取字符串中字母,中文,数字,符号,空格的个数

 1 package notebook;
 2 
 3 /**
 4  *  @少成若天性
 5  *  2017年3月5日
 6  *   获取字符串中字母,中文,数字,符号,空格的个数
 7  */
 8 public class StringTest {
 9             public static void main(String[] args) {
10                           //随便乱打一堆字符串
11                 String s = "山东矿机  发  23(*&…………&%#、、呵呵d<。)#)))≦fsdasda";
12                          //定义正则表达式对象
13                 String a = "[0-9]";
14                 String b = "[a-zA-Z]";
15                 String c = "[\u4e00\u9fa5]";
16                 
17                 int d = 0;
18                 int e = 0;
19                 int f = 0;
20                 int g = 0;
21                 String  ;
22                 for (int i = 0; i < str2.length(); i++) {
23                                       //获取每个字符然后进行计数。
24                     str= String.valueOf(str2.charAt(i));
25                     if (str.matches(a)) {
26                         d++;
27                     }
28                     if (str.matches(b)) {
29                         e++;
30                     }
31                     if (str.matches(c)) {
32                         f++;
33                     }
34                     if (str.equals(" ")) {
35                         g++;
36                     }
37                 }
38                 System.out.println("数字的个数:"+d);
39                 System.out.println("字母的个数:"+e);
40                 System.out.println("中文的个数:"+f);
41                 System.out.println("空格的个数:"+g);
42                 System.out.println("字符的个数"+(str2.length()-d-e-f-g));
43                 
44             }
45 }
46     

 

posted @ 2017-03-05 23:50  少成若天性  阅读(293)  评论(0)    收藏  举报