日期:2014-05-20  浏览次数:20710 次

请问如何在字符串中取指定位数的整数?
如:dsfdf(df0078025sdf)5623 这一串字符串,我只要取 0078025 这长度为连续7位的整数,请各位帮帮忙。

------解决方案--------------------
如果确定字符串中数字的起始和结尾位置, 就可以按照楼上的方法。
如果不确定起始位置,可以参考正则表达式:
String regex = "[\\d]{7}";
String s = "dsfdf(df0078025sdf)5623"; 
Matcher matcher = Pattern.compile(regex).matcher(s);
while(matcher.find())
{
String digit = matcher.group();
int n = Integer.parseInt(digit);
}
------解决方案--------------------
楼主用的是"如"

这种东西还是正则解决...

Java code
public class Test81 {
    public static void main(String[] args) {
        String s = "dfsd0078025sdf)5623sdfs7872226e19sd";
        String[] as = s.split("((?<=(?<=^|\\D+)\\d{7}(?=\\D+|$))|^).*?((?=(?<=^|\\D+)\\d{7}(?=\\D+|$))|$)");
        for (int i = 1; i < as.length; i++)
            System.out.println(as[i]);
    }
}