如何从java中的输入字符串中提取数值 [英] how to extract numeric values from input string in java
本文介绍了如何从java中的输入字符串中提取数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何仅从输入字符串中提取数值?
How can I extract only the numeric values from the input string?
例如,输入字符串可能如下所示:
For example, the input string may be like this:
String str="abc d 1234567890pqr 54897";
我只想要数值,即1234567890
和54897
。所有字母和特殊字符都将被丢弃。
I want the numeric values only i.e, "1234567890"
and "54897"
. All the alphabetic and special characters will be discarded.
推荐答案
String str=" abc d 1234567890pqr 54897";
Pattern pattern = Pattern.compile("\\w+([0-9]+)\\w+([0-9]+)");
Matcher matcher = pattern.matcher(str);
for(int i = 0 ; i < matcher.groupCount(); i++) {
matcher.find();
System.out.println(matcher.group());
}
这篇关于如何从java中的输入字符串中提取数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文