【发布时间】:2016-08-16 16:33:49
【问题描述】:
比方说,
String a="90 results";
我需要提取整数值。
【问题讨论】:
比方说,
String a="90 results";
我需要提取整数值。
【问题讨论】:
【讨论】:
如果整数总是用空格隔开,那么可以拆分字符串
String res[] = a.split(" ");
int n = Integer.parseInt(res[0]);
【讨论】:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String str = "90 results";
Matcher matcher = Pattern.compile("\\d+").matcher(str);
if (!matcher.find())
throw new NumberFormatException("For input string [" + str + "]");
System.out.println(Integer.parseInt(matcher.group()));
}
}
【讨论】: