數目的字符結束我試圖檢查一個字符串的起始和使用正則表達式
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
而且如果條款不會被調用。
數目的字符結束我試圖檢查一個字符串的起始和使用正則表達式
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
而且如果條款不會被調用。
不要使用正則表達式:
Character.isDigit(string.charAt(0)) &&
Character.isDigit(string.charAt(string.length()-1))
方法startsWith()
和endsWith()
類String
只接受字符串,而不是正則表達式。
非常真實,但就目前而言,這是比評論更多的評論。你會如何建議他們「...... [檢查]字符串是否以數字字符開頭和結尾。」? – Leigh
您可以使用matches
方法上String
正是如此:
public static void main(String[] args) throws Exception {
System.out.println("123456".matches("^\\d.*?\\d$"));
System.out.println("123456A".matches("^\\d.*?\\d$"));
System.out.println("A123456".matches("^\\d.*?\\d$"));
System.out.println("A123456A".matches("^\\d.*?\\d$"));
}
輸出:
true
false
false
false
請遵循代碼段。
String variableString = "012testString";
Character.isDigit(string.charAt(0)) && variableString.Any(c => char.IsUpper(c));
是什麼讓你相信這是一個正則表達式的論點? –
變量名稱在哪裏? –
這只是我還是在這個帖子中沒有問題? – Akunosh