public class Demo01 {
public static void main(String[] args) {
// 利用Character类提取字符串中的大写字母和小写字母
String string = "I am Tom.I am from China.I love China!";
// 定义两个字符串存储大、小写字母
String Upper_case = "";
String Lower_case = "";
for (int i = 0; i < string.length(); i++) {
//先将String类型的字符串转换成char类型,再获取每一个字符元素,用charAt(i)实现
if (Character.isUpperCase(string.charAt(i))) {
Upper_case += string.charAt(i) + " ";
} else if (Character.isLowerCase(string.charAt(i))) {
Lower_case += string.charAt(i) + " ";
}
}
System.out.println("字符串中大写字母有:" + Upper_case);
System.out.println("字符串中小写字母有:" + Lower_case);
}
}
输出结果:
字符串中大写字母有:I T I C I C
字符串中小写字母有:a m o m a m f r o m h i n a l o v e h i n a