开发者

Integer to String conversion methods

What are the alter开发者_高级运维native methods for converting and integer to a string?


Integer.toString(your_int_value);

or

your_int_value+"";

and, of course, Java Docs should be best friend in this case.


String one = Integer.toString(1);


String myString = Integer.toString(myInt);


Here are all the different versions:

a) Convert an Integer to a String

Integer one = Integer.valueOf(1);
String oneAsString = one.toString();

b) Convert an int to a String

int one = 1;
String oneAsString = String.valueOf(one);

c) Convert a String to an Integer

String oneAsString = "1";
Integer one = Integer.valueOf(oneAsString);

d) Convert a String to an int

String oneAsString = "1";
int one = Integer.parseInt(oneAsString);

There is also a page in the Sun Java tutorial called Converting between Numbers and Strings.


String.valueOf(anyInt);


There is Integer.toString() or you can use string concatenation where 1st operand is string (even empty): String snr = "" + nr;. This can be useful if you want to add more items to String variable.


You can use String.valueOf(thenumber) for conversion. But if you plan to add another word converting is not nessesary. You can have something like this:
String string = "Number: " + 1
This will make the string equal Number: 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜