星空网 > 软件开发 > 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.     }  



原标题:统计一段文字中数组、中文、英文字母、空格以及其他特殊字符出现的次数

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

下月起,辱骂Shopee员工或买家将被扣分:https://www.goluckyvip.com/news/5933.html
在TikTok发布视频时如何利用标签提高销量?:https://www.goluckyvip.com/news/5934.html
极兔的另一盘棋?Jet Commerce的东南亚电商征途:https://www.goluckyvip.com/news/5935.html
现场直击!CJ物流今起正式罢工,韩国年末配送......:https://www.goluckyvip.com/news/5936.html
Lazada上线LazPick精选商家计划,普通商家的救星来了!:https://www.goluckyvip.com/news/5937.html
中国海关总署:RCEP实施在即 海关准备就绪:https://www.goluckyvip.com/news/5938.html
亚马逊掀起算法革命:A9已死 COSMO当立? :https://www.kjdsnews.com/a/1836493.html
2月新茶饮开店腰斩 加盟门槛一降再降 :https://www.kjdsnews.com/a/1836494.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流