Reputation: 25
I have a string that says "12345". I want to take the 3rd element from the string to convert it to integer (which in this case, the 3rd element would be '3'). How would I do so ?
I heard that you can use Integer.parseInt(s) but this will return the whole integer of the String. I just want one element from it, and at the x-th position.
Upvotes: 0
Views: 312
Reputation: 805
The [charAt() method] (http://www.w3schools.com/jsref/jsref_charat.asp) should get you what you need
Upvotes: 1
Reputation: 413
you can do some thing like that...
int number = Integer.parseInt(givenString.charAt(xPosition));
Hope it helps.
Upvotes: 1
Reputation: 394
I think the easiest way would be to simply use String.SubString. Something like:
string number = "12345";
string element = number.SubString(2, 1);
Where 2 is the position of the third character (remember it's 0 indexed) and 1 is the number of characters to return. You can then turn that into an integer if you want.
Upvotes: 1