题解 | 4行 极简代码#把字符串转换成整数#
把字符串转换成整数
http://www.nowcoder.com/practice/1277c681251b4372bdef344468e4f26e
public int StrToInt(String str) { if(str.length() == 0 || str == null || (str.length() == 1 && (str.charAt(0) == '+' || str.charAt(0) == '-'))) return 0; char[] arr = str.toCharArray(); for(char c : arr) if(c != '+' && c != '-' && !(c >= '0' && c <= '9')) return 0; //不为正负号或数字 return Integer.valueOf(new String(arr)); }