Alec Alec - 19 days ago 5
Java Question

How to get an integer at a specified position in a string of integers?

If I have a string like

String myString = "12345"
and I want to return the value at index [0]. How do I do this? I've tried for example
int foo = myString.charAt(0)
and get a weird value like 49?

Answer

Use Character.getNumericValue():

String myString = "12345";
int foo = Character.getNumericValue(myString.charAt(0));