統計一段文字中數組、中文、英文字母、空格以及其餘特殊字符出現的次數

  1. package util;  
  2.   
  3.   
  4. public class CountStr {  
  5.     /** 
  6.      * 有一個字符串,其中包含中文字符、英文字符和數字字符,請統計和打印出各個字符的個數 
  7.      * 短信發送平臺,短信字數控制查詢方法 
  8.      */  
  9.         public static void main(String[] args) {  
  10.   
  11.             //String str = "adasf AAADFD我是中文,,》123";  
  12.             //String str = "金馬甲高端商品交易平臺--2013全城熱戀克拉鑽石項目預售,18個月,三萬起步,年化8%,預購請致電展恆私人財富:18611297979";  
  13.             String str = " 展恆理財,2004年在北京成立,是國內最大的理財諮詢類機構之一。得到國家頒發的獨立基金銷售牌照.是2013年中國網球公開賽10大核心贊助商之一。 公司成立10年來,在爲客戶進行全面的家庭財務規劃方面積累了十分豐富的經驗。目前擁有中高端忠實客戶10000多名,配置客戶資金超過200億元,位列 行業排名前三強。";  
  14.               
  15.             System.out.println("[總字符數1]:"+countSum(str));  
  16.             System.out.println("--------------------");               
  17.             System.out.println("[總字符數2]:"+countSum2(str));    
  18.             System.out.println("--------------------");               
  19.             System.out.println("[總字符數3]:"+str.length());  
  20.         }  
  21.           
  22.         public static int countSum(String str) {  
  23.             int unicodeCount = 0;  
  24.             int szCount = 0;  
  25.             int zmCount = 0;  
  26.   
  27.             for (int i = 0; i < str.length(); i++) {  
  28.   
  29.                 char c = str.charAt(i);  
  30.                 if (c >= '0' && c <= '9') {  
  31.                     szCount++;  
  32.                 }else if((c >= 'a' && c<='z') || (c >= 'A' && c<='Z')){  
  33.                     zmCount++;  
  34.                 }else{  
  35.                     unicodeCount++;  
  36.                 }  
  37.             }  
  38.             System.out.println("Unicode:"+unicodeCount);  
  39.             System.out.println("數字:"+szCount);  
  40.             System.out.println("字母:"+zmCount);            
  41.             int sum=szCount+zmCount+unicodeCount;  
  42.             return sum;  
  43.         }     
  44.         public static int countSum2(String str) {  
  45.             int abccount = 0;  
  46.             int numcount = 0;  
  47.             int spacecount = 0;  
  48.             int othercount = 0;  
  49.             char[] b = str.toCharArray();  
  50.             for(int i = 0; i < b.length; i++){  
  51.                 if(b[i]>='a'&&b[i]<='z'||b[i]>='A'&&b[i]<='Z'){  
  52.                     abccount++;  
  53.                 }else if(b[i]>='0'&&b[i]<='9'){  
  54.                     numcount++;  
  55.                 }else if(b[i]==' '){  
  56.                     spacecount++;  
  57.                 }else{  
  58.                     othercount++;  
  59.                 }  
  60.         }  
  61.             int sum=abccount+numcount+spacecount+othercount;  
  62.             System.out.println("字符串中含有的英文字母數爲:" + abccount);  
  63.             System.out.println("字符串中含有的數字數爲:" + numcount);  
  64.             System.out.println("字符串中含有的空格數爲:" + spacecount);  
  65.             System.out.println("字符串中含有的其餘字符爲:" + othercount);  
  66.             return sum;   
  67.     }  
相關文章
相關標籤/搜索