String s = "dsfdf(df0078025sdf)5623"; int n = Integer.parseInt(s.substring(8,14));
使用String类的substring(int beginIndex, int endIndex)方法 String s = "dsfdf(df0078025sdf)5623".substring(8,15); 就可以得到----0078025
嗯。应该是substring(8,15)。不是14,疏忽了。
如果确定字符串中数字的起始和结尾位置, 就可以按照楼上的方法。 如果不确定起始位置,可以参考正则表达式: 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); }
public CharSequence subSequence(int beginIndex,int endIndex)beginIndex - 起始索引(包括)endIndex - 结束索引(不包括)。
楼主用的是"如"这种东西还是正则解决...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]); } }(?<=^|\\D+)\\d{7}(?=\\D+|$)匹配7个连续数字,由于太乱,暂把这堆称作"连续7个数字" 那么split后面那堆就是 ((?<=连续7个数字)|^).*?((?=连续7个数字)|$) 这个用来匹配连续7个数字间的东西,把开头和结尾也算进去了.这样分割完,除了几个连续7个数字,开头结尾各有一个"空" 结尾那个被自动扔掉了,开头那个没有, 所以for从1开始就好了.没做太多测试
String strlen = "dsfdf(df0078025sdf)5623 "; int n = Integer.parseInt(strlen.substring(8,14));
int n = Integer.parseInt(s.substring(8,14));
String s = "dsfdf(df0078025sdf)5623".substring(8,15); 就可以得到----0078025
如果不确定起始位置,可以参考正则表达式:
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);
}
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]);
}
}(?<=^|\\D+)\\d{7}(?=\\D+|$)匹配7个连续数字,由于太乱,暂把这堆称作"连续7个数字"
那么split后面那堆就是
((?<=连续7个数字)|^).*?((?=连续7个数字)|$)
这个用来匹配连续7个数字间的东西,把开头和结尾也算进去了.这样分割完,除了几个连续7个数字,开头结尾各有一个"空"
结尾那个被自动扔掉了,开头那个没有, 所以for从1开始就好了.没做太多测试
int n = Integer.parseInt(strlen.substring(8,14));