How can I store a int
value in a String
?
For example:
int number = 10;
string word = number;
How can I store a int
value in a String
?
For example:
int number = 10;
string word = number;
You can use the static method:
String.valueOf(number)
Or
new Integer(number).toString();
int number = 10;
string word = Integer.toString(number);
this will convert the integer "number" to string to be able to store it inside a string without changing it's value just that easy, good luck :)
There are multiple ways to convert an int to string.
String word = Integer.toString(number);
String word = String.valueOf(number);
String word = new Integer(number).toString();
String word = String.format ("%d", number);
etc..
There are more ways to do this. But I prefer the 1st one. Just do some googling you will find more answers! :)
Normal ways would be Integer.toString(number)
or String.valueOf(number)
.
But, You can try this:
int number = 10;
String str = String.valueOf(number);
Or
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(number);
String str = sb.toString();