【发布时间】:2011-10-14 07:05:43
【问题描述】:
如何匹配字符串中的数字整数并返回整数值。? 例如;
String = "Color Red, Size 32 / Text text";
我想从这个字符串中得到“32”整数值。
非常感谢。
问候, 可可
【问题讨论】:
如何匹配字符串中的数字整数并返回整数值。? 例如;
String = "Color Red, Size 32 / Text text";
我想从这个字符串中得到“32”整数值。
非常感谢。
问候, 可可
【问题讨论】:
public static void main(String[] args) {
String s = "Color Red, Size 32 / Text text";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
if (matcher.find()) {
String find = matcher.group();
Integer i = Integer.parseInt(find);
}
}
【讨论】:
试试这个代码
Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher m = p.matcher("Testing1234Testing");
if (m.find()) {
System.out.println(m.group(1));
}
【讨论】: