我需要提取a中找到的第一个整数,java.lang.String并且不确定是否尝试使用substring方法或正则表达式方法:
// Want to extract the 510 into an int.
String extract = "PowerFactor510";
// Either:
int num = Integer.valueof(extract.substring(???));
// Or a regex solution, something like:
String regex = "\\d+";
Matcher matcher = new Matcher(regex);
int num = matcher.find(extract);
Run Code Online (Sandbox Code Playgroud)
所以我问:
注意:字符串将始终以单词PowerFactor后跟非负整数开头.提前致谢!
该字符串将始终以单词"PowerFactor"开头,后跟非负整数
这意味着你确切地知道你会在哪个索引找到这个数字,我想你最好直接使用子字符串,至少考虑一下它会比搜索和匹配工作快得多.
extract.substring("PowerFactor".length());
Run Code Online (Sandbox Code Playgroud)
我找不到任何直接的比较,但你可以阅读以下两个选项中的每一个: