String.replaceAll()在统计大小写字母个数中的应用
来源:互联网 发布:《算法》第4版 编辑:程序博客网 时间:2024/06/06 13:22
给定一个字符串,要统计其中大小写字母的个数。一般来说会用Character类中的isUpperCase和isLowerCase方法:
public class Count{
public static void main(String []args){
String s = "2334badDDFDc%$(23";
int strUpper = 0;
int strLower = 0;
int strOther = 0;
for( int i = 0; i < s.length(); i++){
if( Character.isUpperCase( s.charAt(i) ) == true)
strUpper++;
else if( Character.isLowerCase( s.charAt(i) ) == true)
strLower++;
else
strOther++;
}
System.out.println( "字符串总长度:" +s.length() );
System.out.println( "大写英文字母总个数:" +strUpper );
System.out.println( "小写英文字母总个数:" +strLower);
System.out.println( "非英文字母总个数:" +strOther);
}
}
public static void main(String []args){
String s = "2334badDDFDc%$(23";
int strUpper = 0;
int strLower = 0;
int strOther = 0;
for( int i = 0; i < s.length(); i++){
if( Character.isUpperCase( s.charAt(i) ) == true)
strUpper++;
else if( Character.isLowerCase( s.charAt(i) ) == true)
strLower++;
else
strOther++;
}
System.out.println( "字符串总长度:" +s.length() );
System.out.println( "大写英文字母总个数:" +strUpper );
System.out.println( "小写英文字母总个数:" +strLower);
System.out.println( "非英文字母总个数:" +strOther);
}
}
然而,用String类的replaceAll方法也可以实现这一目标,而且程序看起来更为简单:
public class Count{
public static void main(String []args){
String str = "asdSFfsS#!@#DFa223sdD$Fsf";
String str_Upper = "";
String str_Lower = "";
String str_Other = "";
str_Upper = str.replaceAll( "[^A-Z]","" );
str_Lower = str.replaceAll( "[^a-z]","" );
str_Other = str.replaceAll( "[a-zA-Z]","" );
System.out.println( "字符串总长度:"+str.length() );
System.out.println( "大写英文字母总个数:"+str_Upper.length() );
System.out.println( "小写英文字母总个数:"+str_Lower.length() );
System.out.println( "非英文字母总个数:"+str_Other.length() );
public static void main(String []args){
String str = "asdSFfsS#!@#DFa223sdD$Fsf";
String str_Upper = "";
String str_Lower = "";
String str_Other = "";
str_Upper = str.replaceAll( "[^A-Z]","" );
str_Lower = str.replaceAll( "[^a-z]","" );
str_Other = str.replaceAll( "[a-zA-Z]","" );
System.out.println( "字符串总长度:"+str.length() );
System.out.println( "大写英文字母总个数:"+str_Upper.length() );
System.out.println( "小写英文字母总个数:"+str_Lower.length() );
System.out.println( "非英文字母总个数:"+str_Other.length() );
}
}
- String.replaceAll()在统计大小写字母个数中的应用
- 统计大小写字母个数
- 统计一个字符串中的大小写字母个数…
- 统计字符串中大小写字母的个数
- 第十四周 统计大小写字母的个数
- 统计字符串中的字母个数
- 字符串大统计【二】——统计字符串中的大小写字母个数
- 13周。。。项目五。。统计大小写字母个数
- 第13周项目5-统计大小写字母个数
- 第十三周项目五:统计大小写字母个数
- java统计字符串中数字、大小写字母、空格个数
- 字符串大小写字母个数
- 【2033】统计字符串中的字母个数
- 不分区大小写字母个数统计及其它字符个数统计
- 统计一个字符串中大小写字母的个数,数字出现的个数
- 1051 字母个数统计
- 1251: 统计字母个数
- Python - 统计字母个数
- MySQL中修改密码及访问限制设置详解
- 业务实体对象(Business Entity Object)的序列化
- 把aspx页面生成word文档时怎样取消css?
- ABAP中控制windows剪贴板的方法
- February/March Orcas CTP Now Available
- String.replaceAll()在统计大小写字母个数中的应用
- Error spawning xicl6.exe 卸载Intel C++ Compiler后Visual C++ 6.0不能工作了
- 常见计算机英语词汇解释
- 3-6 存贮设置(Storing Settings)
- Tomcat 4.1中出现的错误一例
- mysqldump
- 网络英语词汇漫谈
- 软件组织的质量治理
- 详述XmlHttp