3 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
代碼 :
public class Example
{
public static void main(String[] args) {
String str ="abc123";
for(int i = 0; i < str.length(); i++) {
System.out.println(Character.isDigit(str.charAt(i)) ? "number" : "alphabet");
}
}
}
輸出 :
alphabet
alphabet
alphabet
number
number
number

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
將您的輸入字符串傳遞到下面的代碼段,如果它的數(shù)字那么它將返回 true 如果它的字母那么它返回 false
public static boolean isNumeric(String str) {
try {
double d = Double.parseDouble(str);
} catch (NumberFormatException | NullPointerException nfe) {
return false;
}
return true;
}
另一種選擇是使用正則表達(dá)式
public static boolean isNumeric(String strNum) {
return strNum.matches("-?\\d+(\\.\\d+)?");
}

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
String.contains可能是你要找的。
String str = "abc";
boolean isAlphabet = str.contains("a"); // is true
boolean isNumber = str.contains("1"); // is false
添加回答
舉報(bào)