zoukankan      html  css  js  c++  java
  • 统计一段文字中数组、中文、英文字母、空格以及其他特殊字符出现的次数

    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.     }  
  • 相关阅读:
    稀疏自编码器一览表
    ZOJ 3886 Nico Number(筛素数+Love(线)Live(段)树)
    K好数(DP)
    【BZOJ4025】二分图
    又一次认识java(七) ---- final keyword
    二分查找
    从朴素贝叶斯分类器到贝叶斯网络(下)
    最近感到深深的绝望,感觉自己太菜了
    leetcode No.19 删除链表的倒数第N个节点 (python3实现)
    leetcode No.94 二叉树的中序遍历 (python3实现)
  • 原文地址:https://www.cnblogs.com/zhangxiongboke/p/5016867.html
Copyright © 2011-2022 走看看