本文實例講述了Java簡單統計字符串中漢字,英文字母及數字數量的方法。分享給大家供大家參考,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package org.zhy.demo.algorithm; /** * 有一個字符串,其中包含中文字符、英文字符和數字字符,請統計和打印出各個字符的個數 * * @author Administrator * */ public class Str { public static void main(String[] args) { String str = "adasfAAADFD阿薩德發123" ; int unicodeCount = 0 ; int szCount = 0 ; int zmCount = 0 ; for ( int i = 0 ; i < str.length(); i++) { char c = str.charAt(i); if (c >= '0' && c <= '9' ) { szCount++; } else if ((c >= 'a' && c<= 'z' ) || (c >= 'A' && c<= 'Z' )){ zmCount++; } else { unicodeCount++; } } System.out.println(unicodeCount); System.out.println(szCount); System.out.println(zmCount); } } |
希望本文所述對大家java程序設計有所幫助。