java-统计字符串中各字符次数
package com.day5.test;
public class Test2 {
/**
* @param args
* 需求:統計字符串中大寫字母字符,小寫字母字符,數字字符出現的次數,其他字符出現的次數
* ABCDEabcd123456!@#$%^
? 注意if..else if...else與if...if...if...else的區別
*/
public static void main(String[] args) {
String s="ABCDEabcd123456!@#$%^&";
int big=0;
int small=0;
int num=0;
int other=0;
/*for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c>='A'&&c<='Z')
big++;
if(c>='a'&&c<='z')
small++;
if(c>='0'&&c<='9')//注意不能寫成if(c>=0&&c<=9)
num++;
else
other++;
}
System.out.println(big);//5
System.out.println(small);//4
System.out.println(num);//6
System.out.println(other);//16
*/
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c>='A'&&c<='Z')
big++;
else if(c>='a'&&c<='z')
small++;
else if(c>='0'&&c<='9')//注意不能寫成if(c>=0&&c<=9)
num++;
else
other++;
}
System.out.println(big);//5
System.out.println(small);//4
System.out.println(num);//6
System.out.println(other);//7
}
}
轉載于:https://www.cnblogs.com/zhujialei123/p/8111474.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的java-统计字符串中各字符次数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: left join 与left oute
- 下一篇: nprogress.js 头部进度条使用