String2Int

    public int StrToInt(String str) {
        if(str.length()<= 0){
           return 0;
        }
        int firstIndex = 0;
        boolean isNegative = false;
        char first = str.charAt(0);
        if(first == '+'|| first == '-'){
            if(first == '-'){
                isNegative = true;
            }
            firstIndex ++;
        }
        int index = firstIndex;
        int value = 0;
        while(index < str.length()){
            int newValue = str.charAt(index) - '0';
            if(newValue >= 0 && newValue<=9){
                value = value*10+ newValue;
            }else{
                return 0;
            }
            index ++;
        }
        if(isNegative){
             return -value;
        }else{
            return value;
        }
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326934094&siteId=291194637