题解 | #把字符串转换成整数#
把字符串转换成整数
http://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e
public class Solution {
public int StrToInt(String str) {
if(str==null || str.length()==0){
return 0;
}
char[] chars=str.toCharArray();
//判断第一个字符:应该为+-,或者数字,再判断接下来的每个字符是否为数字
if((chars[0]=='+'||chars[0]=='-')||(chars[0]>='0' && chars[0]<='9')){
for(int i=1;i<chars.length;i++){
if(chars[i]>='0' && chars[i]<='9'){
return Integer.parseInt(str);
}else{
return 0;
}
}
}
return 0;
}
}
