找出字符串有有多少个大写字母、小写字母及其它
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 + "其它字母"); } }