我有一个String变量(基本上是一个带有未指定数字的英文句子),我想将所有数字提取到一个整数数组中.我想知道是否有正则表达式的快速解决方案?
我使用了Sean的解决方案并稍微改了一下:
LinkedList<String> numbers = new LinkedList<String>();
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(line);
while (m.find()) {
numbers.add(m.group());
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,其中包含一个int值.我只想从字符串中提取int值并打印.
String str="No. of Days : 365";
String daysWithSplChar = str.replaceAll("[a-z][A-Z]","").trim();
char[] ch = daysWithSplChar.toCharArray();
StringBuffer stb = new StringBuffer();
for(char c : ch)
{
if(c >= '0' && c <= '9')
{
stb.append(c);
}
}
int days = Integer.ParseInt(stb.toString());
Run Code Online (Sandbox Code Playgroud)
有没有比这更好的方法.请告诉我.