Java: parse int value from a char

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

    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.

提交回复
热议问题