Java中 Character方法练习:字符串中英文字母个数 5435abc54abc3AHJ5 正则:matches("[a-zA-Z0-9]{1}")

package com.swift;

public class String_Letter_Number_Test {

    public static void main(String[] args) {
        /*
         * 字符串中英文字母个数 5435abc54abc3AHJ5
         */
        String str="5435abc54abc3AHJ5";
        char[] arr=str.toCharArray();
        
        int num=0;
        for(char c:arr) {
            if(Character.isLetter(c)) {
                num++;
            }
        }
        System.out.println("The numbers of letter int the String is :"+num);

    }

}

 正则 

package com.swift;

public class String_Letter_Number_Test {

    public static void main(String[] args) {
        /*
         * 字符串中英文字母和数字的个数 5435abc54abc3AHJ5
         */
        String str="5435ab,;‘’c54abc,.?/3AHJ5";
        char[] arr=str.toCharArray();
        
        int num=0;
        for(char c:arr) {
//            if(Character.isLetter(c)) {
//                num++;
//            }
            
            System.out.println(c);
            if(String.valueOf(c).matches("[a-zA-Z0-9]{1}")) {  //  "\\w" 表示匹配字母数字下划线
                num++;
            }
        }
        System.out.println("The numbers of letter int the String is :"+num);

    }

}

 

posted @ 2018-01-16 21:02  Advancing-Swift  阅读(414)  评论(0编辑  收藏  举报