string转换成int的算法

来源:互联网 发布:淘宝上1元秒杀是真的吗 编辑:程序博客网 时间:2024/04/30 13:02
public static int stringToInt(String input) {if (!isValidString(input)) {throw new IllegalArgumentException("input is not a number string!!");}boolean isNegative = isNegative(input);if (isNegative) {input = input.substring(1);}char[] inputArray = input.toCharArray();// 321 ,-89int total = 0;int step = 1;for (int i = inputArray.length - 1; i >= 0; i--) {total += (inputArray[i] - '0') * step;step *= 10;}return isNegative ? 0 - total : total;}

0 0
原创粉丝点击