String转int 非库函数 Java

来源:互联网 发布:矩阵制组织结构的特点 编辑:程序博客网 时间:2024/05/29 15:01

public class Solution {

public static int StringConvertToInt(String num) throws Exception {    char[] ch = num.toCharArray();    int i = 0, value = 0;    while (i < ch.length)    {        if (ch[i] == ' ') {            i++; continue;        }        if (ch[i] < '0' || ch[i] > '9')            throw new Exception("node number");        value = value * 10 + (ch[i] - '0');        i++;    }    return value;}public static void main(String[] args) throws Exception {    String str = "123 456";    System.out.println(StringConvertToInt(str));}

}

输出:
123456

原创粉丝点击