Java: parse int value from a char

后端 未结 7 1181
借酒劲吻你
借酒劲吻你 2020-11-22 16:10

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).



        
7条回答
  •  失恋的感觉
    2020-11-22 16:45

    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.

提交回复
热议问题