-5

我需要提取字符串中的整数值。

例如

this is the java 1234 and 7899/45767 program

因为我只需要提取整数

1234
7899
45767
4

3 回答 3

2

I think a regex could fit your need:

import java.util.regex.*;

String aParser="123 bla bla 1234 bla bla 0324";
Pattern p=Pattern.compile("(\d+)");
Matcher m=p.matcher(aParser);

while(m.find())
{ 
   //your code here
}
于 2013-08-01T11:54:18.337 回答
0

尝试这个

    Matcher m = Pattern.compile("-?\\d+").matcher(str);
    while(m.find()) {
        System.out.println(m.group());
    }
于 2013-08-01T11:55:46.140 回答
0

你可以试试Scanner.nextInt()方法

Scanner sc = new Scanner("this is the java 1234 and 7899/45767 program, in that i need to extract only integer like 1234 7899 45767");
while(sc.hasNext()) {
   boolean scanned = false;
   if(sc.hasNextInt()) {
      System.out.println(sc.nextInt());
      scanned = true;
   }
   if(!scanned)
     sc.next();
}
于 2013-08-01T11:50:59.377 回答