Java統計字符串中漢字,英文,數字,特殊符號個數
package wzs.arithmetics;/**
- 分別統計出其中字符串中漢字,英文字母,數字,其他字符數量
@author wWX154783 / public class Test_wzs7 { public static void main(String[] args) {
String str = "a12中國3@b&4語*言3c"; String E1 = "[\u4e00-\u9fa5]";// 中文 String E2 = "[a-zA-Z]";// 英文 String E3 = "[0-9]";// 數字 int chineseCount = 0; int englishCount = 0; int numberCount = 0; String temp; for (int i = 0; i < str.length(); i++) { temp = String.valueOf(str.charAt(i)); if (temp.matches(E1)) { chineseCount++; } if (temp.matches(E2)) { englishCount++; } if (temp.matches(E3)) { numberCount++; } } System.out.println("漢字數:" + chineseCount); System.out.println("英文數:" + englishCount); System.out.println("數字數:" + numberCount); System.out.println("特殊字符:" + (str.length() - (chineseCount + englishCount + numberCount)));
} }</pre>
本文由用戶 cwf8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!