码迷,mamicode.com
首页 > 其他好文 > 详细

统计一个字符串中字母、空格和数字的个数

时间:2014-11-29 10:22:59      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   for   div   log   bs   

public class Test3 {

    public static void main(String[] args) {
        String s1 = "I am";
        String s2 = " a student";
        String s3;
        String s4;
        s3 = s1 + s2;
        s4 = s3.substring(7, 8);
        //System.out.println(s3);//连接两个字符串
        //System.out.println(s4);//求出第7个字符
        //System.out.println(s3.indexOf("am"));//找出“am”在该字符串中的位置
        //System.out.println(s3.toUpperCase());//把所有字符都变成大写字符
        int a = 0,b = 0,c = 0;
        for(int i = 0; i < s3.length(); i++){
            //48-57代表0-9
            if(s3.charAt(i) <= 57 & s3.charAt(i) >= 48){
                a++;
            }
            //65-90代表A-Z,97-122代表a-z
            if(s3.charAt(i) <= 90& s3.charAt(i)>=65|s3.charAt(i) <=122&s3.charAt(i) >=97){
                b++;
            }
            if(s3.charAt(i) == ‘ ‘){
                c++;
            }
            
        }
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }

}

 

统计一个字符串中字母、空格和数字的个数

标签:style   blog   ar   color   sp   for   div   log   bs   

原文地址:http://www.cnblogs.com/wxh04/p/4130079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!