将字符串中的数字和字母分别输出,可以重复 java实现

2025-05-23 20:10:41
推荐回答(1个)
回答1:

public static void print(String str){
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
int length = str.length();

for(int i=0;i char temp = str.charAt(i);
if(Character.isDigit(temp)){
sb1.append(temp);
}else {
sb2.append(temp);
}
}
System.out.println("数字:" + sb1.toString());
System.out.println("字母:" + sb2.toString());
}

public static void main(String[] args) {
String s = "a2b3123c40";
print(s);
}