遍历字符串的方法
1,将字符串转化为字符数组,String s = "HelloWorld";
char[] chs = s.toCharArray();
for(int i = 0;i
System.out.print(chs[i] + " ");
}
2,使用 charAt(int index)方法String s = "HelloWorld";
for(int i = 0;i
System.out.print(s.char[i] + " ");
}
统计思想
统计字符串中 大小写和数字出现的次数public static void count(String s){
int lowerCount = 0;
int upperCount = 0;
int other = 0;
for(int i = 0;i
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'){
lowerCount++;
}else if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'){
upperCount++;
}else
other++;
}
System.out.println("lowerCount =" + lowerCount + " upperCount =" + upperCount + " other =" + other);
}