Java: parse int value from a char

566,647

Solution 1

Try Character.getNumericValue(char).

String element = "el5";
int x = Character.getNumericValue(element.charAt(2));
System.out.println("x=" + x);

produces:

x=5

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.

Solution 2

Try the following:

str1="2345";
int x=str1.charAt(2)-'0';
//here x=4;

if u subtract by char '0', the ASCII value needs not to be known.

Solution 3

That's probably the best from the performance point of view, but it's rough:

String element = "el5";
String s;
int x = element.charAt(2)-'0';

It works if you assume your character is a digit, and only in languages always using Unicode, like Java...

Solution 4

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.

String str = "123";

int a=str.charAt(1)-'0';

Solution 5

String a = "jklmn489pjro635ops";

int sum = 0;

String num = "";

boolean notFirst = false;

for (char c : a.toCharArray()) {

    if (Character.isDigit(c)) {
        sum = sum + Character.getNumericValue(c);
        System.out.print((notFirst? " + " : "") + c);
        notFirst = true;
    }
}

System.out.println(" = " + sum);
Share:
566,647

Related videos on Youtube

Noya
Author by

Noya

Main focused on Moblie Computing (Android and iPhone), web programming, video and music production.

Updated on October 12, 2021

Comments

  • Noya
    Noya over 2 years

    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)

  • Bart Kiers
    Bart Kiers about 13 years
    Try that with the string "el५" where is the digit 5 in India. :)
  • Alexis Dufrenoy
    Alexis Dufrenoy about 13 years
    I'm sure you worked hard to find this example... :-) Ok, if you have to parse non-arab digits, avoid this method. Like I said, it's rough. But it's still the fastest method in the 99.999% of cases where it works.
  • Ignacio Chiazzo
    Ignacio Chiazzo over 7 years
    What is the reason about that? the result is the substraction between the '0' in the ASCI and the char in the asci???
  • Istiaque Ahmed
    Istiaque Ahmed about 7 years
    @AlexisDufrenoy, why should subtracting character '0' return the integer value ?
  • WHDeveloper
    WHDeveloper about 7 years
    Look at the values from the ASCII table char '9' = int 57, char '0' = int 48, this results in '9' - '0' = 57 - 48 = 9
  • Kevin Van Ryckegem
    Kevin Van Ryckegem almost 7 years
    @msj Because the values of '0', '1', '2', ... in ascii are ascending. So e.g. '0' in ascii is 48, '1' is 49, etc. So if you take '2' - '0' you really just get 50 - 48 = 2. Have a look at an ASCII table in order to understand this principle better. Also, 'x' means get the ascii value of the character in Java.
  • scoots
    scoots almost 6 years
    @KevinVanRyckegem thank you! I was looking for why - '0' worked...I thought it was some deep magic in how Java interpreted chars or whatever...this truly was a case of me over complicating something...
  • omikes
    omikes over 5 years
    works like a charm. lol @ counterexample and all the indians who copied your answer. the irony.
  • Janac Meena
    Janac Meena almost 4 years
    Although this is a neat trick, it is less readable than using Character.getNumericValue