java中如何把string型字符串中的一部分转换成int型,string型是数字的

2025-06-25 13:38:59
推荐回答(4个)
回答1:


public class Test {

    public static int getNumber(String str,int index,int length){
        int res = -1;
        int len = source.length();
        if(index >= len){
            return res;
        }
        String tmp = source.substring(index,Math.min(index+length,len));
        res = new Integer(tmp);
        return res;
    }
    
    public static void main(String[] args) {
        String a = "12345678900";
        System.out.println(getNumber(a, 4, 3));
    }
}

回答2:

那就先转换成char的字符数组 再控制长度

回答3:

用substring(int beginIndex, int endIndex) 返回个字符串再Integer强转就ok了
或者用正则取值再转

回答4:

类型强转你没学过吗?