public class TestString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] chars = str.toCharArray();
int length = chars.length;
System.out.println("length:"+length);
if(length>0){
System.out.println("该字符串"+str+"中的数字是:");
for (int i = 0; i <chars.length; i++) {
char aChar = chars[i];
if(aChar!=' '){
if (Character.isDigit(aChar)){
System.out.print(aChar+",");
}
}
}
}
}
}
注意:
判断字符的类型:
Character.isDigit(aChar) //判断 aChar 是否是数字。char aChar
isLetter() 方法用于判断指定字符是否为字母。