I just want to know if there's a better solution to parse a number from a character in a string (assuming that we know that the character at index n is a number).
String element = "el5";
String s;
s = ""+element.charAt(2);
int x = Integer.parseInt(s);
//result: x = 5
(useless to say that it's just an example)
By simply subtracting by char '0'(zero) a char (of digit '0' to '9') can be converted into int(0 to 9), e.g., '5'-'0' gives int 5.
Try
Character.getNumericValue(char)
.produces:
The nice thing about
getNumericValue(char)
is that it also works with strings like"el٥"
and"el५"
where٥
and५
are the digits 5 in Eastern Arabic and Hindi/Sanskrit respectively.That's probably the best from the performance point of view, but it's rough:
It works if you assume your character is a digit, and only in languages always using Unicode, like Java...
Try the following:
if u subtract by char '0', the ASCII value needs not to be known.