public int strToInteger(String str) {
try {
double num = 0l;
int len = str.length();
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if(c != ' ' &&(c == '+' || c== '-' && (str.codePointAt(i)>=48 && str.codePointAt(i)<57))){
int j = i+1;
if(j>= len){
return 0;
}
for (int k = 0; k < len; k++) {
if(!(str.codePointAt(i)>=48 && str.codePointAt(i)<57)){
return 0;
}
num = num*10+Integer.parseInt(str.substring(k,k+1));
}
if(c == '-'){
num = -1*num;
}
return (int) num;
}else if(c!=' '){
return 0;
}
}
return (int) num;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
网友评论