天天看點

統計字元串中英文字母、數字、空格和其他字元的個數

統計字元串中英文字母、數字、空格、其他字元的個數?
public static void countNum(String str) {
		int count1 = 0;
		int count2 = 0;
		int count3 = 0;
		int count4 = 0;
		int len = str.length();
		
		for (int i = 0; i < len; i++) {
			char c = str.charAt(i);
			if(c>='a'&& c<='z'|| c>='A'&& c<='Z') {
				count1++;
			}else if(c>='0'&&c<='9') {
				count2++;
			}else if(c==' ') {
				count3++;
			}else {
				count4++;
			}
		}
		System.out.println("字元串長度"+len);
		System.out.println("字母個數"+count1);
		System.out.println("數字個數"+count2);
		System.out.println("空格個數"+count3);
		System.out.println("其他個數"+count4);
	}
	```
           

繼續閱讀