1. 程式人生 > >找出字符串有有多少個大寫字母、小寫字母及其它

找出字符串有有多少個大寫字母、小寫字母及其它

indexof [] amp upper lfa static print pri cde

public class TestStringCharAt {
    /**
     * 找出字符串有有多少個大寫字母、小寫字母及其它
     */
    public static void main(String[] args) {
        String str = "AADDBDDCDEFddlfafdfaifABDILIWcdafd122~!!@AbC";
        char ch = 0;
        int upperNum = 0;
        int lowerNum = 0;
        int otherNum = 0;
        /**
         * 方法一
         *         for (int i=0;i<str.length();i++) {
         *             ch = str.charAt(i);
         *             if (ch >= ‘A‘ && ch <= ‘Z‘) {
         *                 upperNum ++;
         *             }
         *             else if (ch >=‘a‘ && ch <= ‘z‘ ) {
         *                 lowerNum ++;
         *             }else {
         *                 otherNum ++;
         *             }
         *         }
         *
         
*/ //方法二 String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String lower = "abcdefghijklmnopqrstuvwxyz"; for (int i=0;i<str.length();i++) { ch = str.charAt(i); if (upper.indexOf(ch) != -1) { upperNum ++; }else if (lower.indexOf(ch) != -1) { lowerNum
++; }else { otherNum ++; } } System.out.println(upper.indexOf(‘A‘)); System.out.println("共有" + upperNum + "個大寫字母"); System.out.println("共有" + lowerNum + "個小寫字母"); System.out.println("共有" + otherNum + "其它字母"); } }

找出字符串有有多少個大寫字母、小寫字母及其它