2

如何匹配字符串中的数字整数并返回整数值。?例如;

String = "Color Red, Size 32 / Text text";

我想从这个字符串中得到“32”整数值。

提前谢谢了。

问候,科科

4

2 回答 2

4
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);
    }
}
于 2011-07-27T13:53:37.100 回答
0

试试这个代码

Pattern p = Pattern.compile("^[a-zA-Z]+([0-9]+).*");
Matcher m = p.matcher("Testing1234Testing");

if (m.find()) {
    System.out.println(m.group(1));
}
于 2011-07-27T13:51:22.677 回答