Java-将整数转换为字符串
给定一个数字:
int number = 1234;
将其转换为字符串的“最佳”方法是:
String stringNumber = "1234";
我尝试过搜索(谷歌搜索)答案,但似乎没有多少人“值得信赖”。
Given a number:
int number = 1234;
Which would be the "best" way to convert this to a string:
String stringNumber = "1234";
I have tried searching (googling) for an answer but no many seemed "trustworthy".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有多种方式:
String.valueOf(number)
(我的偏好)"" + number
(我不知道编译器如何处理它,也许它是高效的如上所述)Integer.toString(number)
There are multiple ways:
String.valueOf(number)
(my preference)"" + number
(I don't know how the compiler handles it, perhaps it is as efficient as the above)Integer.toString(number)
Integer 类有静态方法 toString() - 您可以使用它:
Integer class has static method toString() - you can use it:
始终使用
String.valueOf(number)
或Integer.toString(number)
。使用“”+数字是一种开销,并且会执行以下操作:
Always use either
String.valueOf(number)
orInteger.toString(number)
.Using "" + number is an overhead and does the following:
这样就可以了。相当值得信赖。 :)
只是为了澄清,除非您正在寻找微观优化,否则这是有效的并且可以使用。
This will do. Pretty trustworthy. : )
Just to clarify, this works and acceptable to use unless you are looking for micro optimization.
我知道如何将整数转换为字符串的方法是使用以下代码:
如果
您有一个整数 i 和一个字符串 s,则以下内容适用:
如果您想将字符串“s”转换为整数“i”,那么以下将起作用:
The way I know how to convert an integer into a string is by using the following code:
and
If you had an integer i, and a string s, then the following would apply:
If you wanted to convert a string "s" into an integer "i", then the following would work:
这是我用来将整数转换为字符串的方法。如果我做错了,请纠正我。
This is the method which i used to convert the integer to string.Correct me if i did wrong.