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));
}
}
那就先转换成char的字符数组 再控制长度
用substring(int beginIndex, int endIndex) 返回个字符串再Integer强转就ok了
或者用正则取值再转
类型强转你没学过吗?