简体   繁体   中英

Integer to String conversion methods

将整数转换为字符串的替代方法有哪些?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM