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;
        }
    }

猜你喜欢

转载自daojin.iteye.com/blog/2391271