|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
String to int???
can somebody plz give me an example of how to turn a string into an int....i look in the java api and couldn't find it
|
|
#2
|
||||
|
||||
|
Code:
String str = "123"; int i = Integer.parseInt(str); |
|
#3
|
|||
|
|||
|
or if you want to do it the easy way:
int i; String str = "123"; i = str - 0; ![]() |
|
#4
|
||||
|
||||
|
Oh sure, but what fun is the easy way?
![]() |
|
#5
|
||||
|
||||
|
@michaelsoft: That is really not possible in Java (nor in C actually). The minus operator is not overloaded to handle strings. In the case of the plus, which can handle strings and ints, the result is a string which you cannot assign to an int.
Another way (though not necessary easier or better) is: String s = "2"; int i = Integer.valueOf(s).intValue(); in java 5 you can even drop the intValue() part: String s = "2"; int i = Integer.valueOf(s); With valueOf you can also get the Wrapper classes for primitive types if you need them. If your number has a different base you can do it like this: String s = "2A"; int i = Integer.parseInt(s,16); |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > String to int??? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|