首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请教怎么在字符串中取指定位数的整数

2012-02-16 
请问如何在字符串中取指定位数的整数?如:dsfdf(df0078025sdf)5623这一串字符串,我只要取 0078025 这长度为

请问如何在字符串中取指定位数的整数?
如: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]);    }} 

热点排行