字符串转int类型


import org.junit.Test;

public class solution {
    @Test
    public void testFunc(){
//    String string = "-10";
    String string = "10";
    int res = parseInt(string);
    System.out.println("res: "+res);
    
    }
    
//    字符串转int类型
    
    public int parseInt(String str){
        char ele = str.charAt(0);
        boolean fuShu = false;
        if (ele=='-') {
            str = str.substring(1);
            fuShu=true;
        }
        char[] charArray = str.toCharArray();
        int res=0;
        for(int i=0;i<charArray.length;i++){
            res=res*10+(charArray[i]-'0');
        }
        if (fuShu) {
            res = 0-res;
        }
        
        return res;
    }
    
}

猜你喜欢

转载自blog.csdn.net/wwzheng16/article/details/81022503
今日推荐