0
如何在輸出+ -12aba到+ -12中使用正則表達式,也就是說,除了數字和負號以外。如何在輸出中使用正則表達式+ -12aba到+ -12
public class LeetCode8 {
public static int myAtoi(String str) {
str = str.replaceAll("\\s+", "");
System.out.println(str);
if (!str.matches("[0-9]+")&&!str.matches("\\+[0-9]+")&&!str.matches("\\-[0-9]+")) {
return 0;
}
str.replaceAll("", "0");
if (str.length() > 10) {
return 0;
}
long a = Long.valueOf(str);
if (a > Integer.MAX_VALUE) {
return 0;
}
return (int) a;
}
public static void main(String[] args) {
int i = myAtoi("-12aba");
System.out.println(i);
//i want wo output -12
}
}
這裏是你的代碼? –
沒有代碼,但我想問的是如何使用正則表達式將字符串+ -12aba轉換爲+ -12。 – Lizumi
所以你還沒有嘗試過任何東西! –