檢查字符串長度的可能方法是什麼?等於最後附加的數字。我附上我寫的任何幫助表示讚賞的代碼。檢查字符串的長度是否等於最後附加的數字
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcd10";
String[] part = str.split("(?<=\\D)(?=\\d)");
String strPart1 = part[0];
int n = str.length();
// Traverse string from end and find the number
// stored at the end.
// x is used to store power of 10.
int num = 0, x = 1, i = n-1;
for (i=n-1; i>=0; i--) {
char c = str.charAt(i);
if ('0' <= c && c <= '9') {
num = (c - '0')*x + num;
x = x * 10;
System.out.println("true");
} else break;
}
}
}
使用'模式'匹配整數。爲它獲取一個「匹配器」。 'start()'返回開始索引。 'group()'方法返回你的數字。 – soufrk