在 Java 中从 String 中获取 int,也包含字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2338790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get int from String, also containing letters, in Java
提问by DisgruntledGoat
How can I get the int value from a string such as 423e
- i.e. a string that contains a number but also maybe a letter?
如何从字符串中获取 int 值,例如423e
- 即包含数字但也可能是字母的字符串?
Integer.parseInt()
fails since the string must be entirely a number.
Integer.parseInt()
失败,因为字符串必须完全是一个数字。
采纳答案by Uri
Unless you're talking about base 16 numbers (for which there's a method to parse as Hex), you need to explicitly separate out the part that you are interested in, and then convert it. After all, what would be the semantics of something like 23e44e11d in base 10?
除非您在谈论基数为 16 的数字(为此有一种方法可以解析为十六进制),否则您需要明确地分离出您感兴趣的部分,然后对其进行转换。毕竟,基数为 10 的 23e44e11d 之类的语义是什么?
Regular expressions could do the trick if you know for sure that you only have one number. Java has a built in regular expression parser.
如果您确定只有一个数字,则正则表达式可以解决问题。Java 有一个内置的正则表达式解析器。
If, on the other hands, your goal is to concatenate all the digits and dump the alphas, then that is fairly straightforward to do by iterating character by character to build a string with StringBuilder, and then parsing that one.
另一方面,如果您的目标是连接所有数字并转储 alpha,那么通过逐个字符迭代以使用 StringBuilder 构建字符串,然后解析该字符串,这是相当简单的。
回答by digiarnie
Perhaps get the size of the string and loop through each character and call isDigit() on each character. If it is a digit, then add it to a string that only collects the numbers before calling Integer.parseInt().
也许获取字符串的大小并遍历每个字符并在每个字符上调用 isDigit() 。如果是数字,则在调用 Integer.parseInt() 之前将其添加到仅收集数字的字符串中。
Something like:
就像是:
String something = "423e";
int length = something.length();
String result = "";
for (int i = 0; i < length; i++) {
Character character = something.charAt(i);
if (Character.isDigit(character)) {
result += character;
}
}
System.out.println("result is: " + result);
回答by polygenelubricants
Replace all non-digit with blank: the remaining string contains only digits.
用空白替换所有非数字:剩余的字符串仅包含数字。
Integer.parseInt(s.replaceAll("[\D]", ""))
This will also remove non-digits inbetween digits, so "x1x1x"
becomes 11
.
这也将删除数字之间的非数字,因此"x1x1x"
变为11
.
If you need to confirm that the string consists of a sequence of digits (at least one) possibly followed a letter, then use this:
如果您需要确认字符串由可能跟随一个字母的一系列数字(至少一个)组成,请使用以下命令:
s.matches("[\d]+[A-Za-z]?")
回答by jarnbjo
The NumberFormat class will only parse the string until it reaches a non-parseable character:
NumberFormat 类只会解析字符串,直到它遇到不可解析的字符:
((Number)NumberFormat.getInstance().parse("123e")).intValue()
will hence return 123.
因此将返回 123。
回答by Claudiu
Just go through the string, building up an int as usual, but ignore non-number characters:
只需遍历字符串,像往常一样构建一个 int,但忽略非数字字符:
int res = 0;
for (int i=0; i < str.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') continue;
res = res * 10 + (c - '0');
}
回答by Soheil Setayeshi
You can also use Scanner :
您还可以使用扫描仪:
Scanner s = new Scanner(MyString);
s.nextInt();