统计字符串里数字,字母,空格数字
2022-05-17 17:18 钟铧若岩 阅读(63) 评论(0) 编辑 收藏 举报
1 @Test 2 public void Test15() { 3 4 String str1 ="Hello wfg , wangufui@sobey.com"; 5 char []arr = str1.toCharArray(); 6 int digital=0, character=0, blank=0, other=0; 7 for (int i = 0; i < arr.length; i++) { 8 if (arr[i] >= '0' && arr[i] <= '9') { 9 digital++; 10 } else if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z')) { 11 character++; 12 } else if (arr[i] == ' ') { 13 blank++; 14 } else { 15 other++; 16 } 17 } 18 System.out.println("数字个数:"+digital); 19 System.out.println("英文字母个数:"+character); 20 System.out.println("空格个数:"+blank); 21 System.out.println("其他字符个数:"+other); 22 23 }