我需要提取字符串中的整数值。
例如
this is the java 1234 and 7899/45767 program
因为我只需要提取整数
1234
7899
45767
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
}
尝试这个
Matcher m = Pattern.compile("-?\\d+").matcher(str);
while(m.find()) {
System.out.println(m.group());
}
你可以试试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();
}