【发布时间】:2017-04-23 21:50:52
【问题描述】:
目前我只能返回 40。
我也想要第二个数字(在本例中为 1,600,077.17)
但之后没有任何东西,例如“1,000.00”或“23”或“70”。
请记住,字符串“str”只是一个示例值,有时会发生变化。
public static String extractNumber1(final String line) {
String str = "cows 40 1,600,077.17 1,000.00 23 70";
if(str == null || str.isEmpty()) return "";
StringBuilder sb = new StringBuilder();
boolean found = false;
for(char c : str.toCharArray()){
if(Character.isDigit(c)){
sb.append(c);
found = true;
} else if(found){
// If we already found a digit before and this char is not a digit, stop looping
break;
}
}
return sb.toString();
}
【问题讨论】:
-
@Ryan 谢谢。
-
添加一个
regex标签会吸引正则表达式大师在这里..