在java中将带空格的字符串转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23851668/
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
Converting a String with spaces to an Integer in java
提问by k1234
I'm trying to convert a String variable into an integer, only the String looks like this (for example):
我正在尝试将 String 变量转换为整数,只有 String 看起来像这样(例如):
String string = " 12";
And so the String has a space in front of it. Now the String variable is being read from a .txt file which is why I'm having the issue of having the space in front of it. I need to convert the variable into an int, but when I try:
所以字符串前面有一个空格。现在正在从 .txt 文件中读取 String 变量,这就是为什么我遇到了前面有空格的问题。我需要将变量转换为 int,但是当我尝试时:
int integer = Integer.parseInt(string);
to pass the String into an integer, it compiles but I get an error when trying to run the program, since there is white space as part of the String.
要将字符串传递为整数,它会编译,但在尝试运行程序时出现错误,因为字符串中有空格。
Is there a way to pass a String of numbers with a space in front of it into an int? Would .trim work? And if so, how would I use that? (I'm not very familiar with .trim, I'm still learning!!) I appreciate the help! ^_^
有没有办法将前面有空格的数字字符串传递给int?.trim 会起作用吗?如果是这样,我将如何使用它?(我对 .trim 不是很熟悉,我还在学习中!!)感谢您的帮助!^_^
采纳答案by Masudul
Would .trim work? And if so, how would I use that?
.trim 会起作用吗?如果是这样,我将如何使用它?
Yes, trim()
will work, it will remove leading and trailing spaces from String
,
是的, trim()
会起作用,它会从String
, 中删除前导和尾随空格
int integer = Integer.parseInt(string.trim());
回答by Josh Engelsma
Use the string trim function in Java to first get a string without the spaces, then parse it...
使用Java中的字符串修剪函数首先得到一个没有空格的字符串,然后解析它...
1.) Remove spaces with trim... String#trim
1.)用修剪删除空格... String#trim
String stringWithoutSpace = string.trim();
2.) Parse the string without spaces... Integer#parseInt
2.)解析没有空格的字符串... Integer#parseInt
int integer = Integer.parseInt(stringWithoutSpace);
3.) Do everything above in one step...
3.)一步完成以上所有事情...
int integer = Integer.parseInt(string.trim());
回答by marcomarandiz
The string split() method works well if you have a string of multiple numbers separated by spaces.
如果您有一串由空格分隔的多个数字,则字符串 split() 方法效果很好。
String numbers = "1 23 456";
String[] numbersArray = numbers.split(" "); // splitting string by spaces
System.out.println(numbersArray.toString());
// output ["1","23","456"]
Then you can use Integer.parseInt() on each index of the array. Hope that helps
然后你可以在数组的每个索引上使用 Integer.parseInt() 。希望有帮助
回答by Alireza Fattahi
To make sure that your code does not throw NumberFormatException
or even NullPointerException
, when you get number from user, use apache common classes:
为了确保您的代码不会抛出NumberFormatException
或什至NullPointerException
,当您从用户那里获取号码时,请使用 apache 公共类:
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
.....
// '1' is the default value which is returned when the string can not be converted to int
// The StringUtils.trimToEmpty do both left and right trim
int number = NumberUtils.toInt(StringUtils.trimToEmpty( " 12 ",1)) = 12;
回答by Gilbert Lopez
If you using Java 11, you can use the String::strip
method. For example:
如果您使用 Java 11,则可以使用该String::strip
方法。例如:
int integer = Integer.parseInt(string.strip());
The strip()
method is similar to trim()
but uses Character.isWhitespace(int)
to resolve spaces. (The Character.isWhitespace(int)
method is Unicode-aware.)
该strip()
方法类似于trim()
但用于Character.isWhitespace(int)
解析空格。(该Character.isWhitespace(int)
方法是 Unicode 感知的。)