Java: parse int value from a char

后端 未结 7 1266
借酒劲吻你
借酒劲吻你 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:53

    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';
    

提交回复
热议问题