本文介绍了从字符串中提取数字-StringUtils Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,我想提取字符串中的(唯一的)数字序列.
I have a String and I want to extract the (only) sequence of digits in the string.
示例:helloThisIsA1234Sample.我想要1234
Example:helloThisIsA1234Sample.I want the 1234
假设数字序列在字符串中只会出现一次,而不会出现在同一位置.
It's a given that the sequence of digits will occur only once within the string but not in the same position.
(对于那些会问的人,我有一个服务器名称,需要在其中提取一个特定的数字)
(for those who will ask, I have a server name and need to extract a specific number within it)
我想使用Apache commomns中的StringUtils类.
I would like to use the StringUtils class from Apache commomns.
谢谢!
推荐答案
使用此代码仅包含您所需的输出.
Use this code numberOnly will contain your desired output.
String str="sdfvsdf68fsdfsf8999fsdf09";
String numberOnly= str.replaceAll("[^0-9]", "");
这篇关于从字符串中提取数字-StringUtils Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!