[Java] int 與 String 互相轉換

Convert int to String

int i;

1.String str = Integer.toString(i);

      a string representation of the argument in base 10.


2.String str = String.valueof(i);

      a string representation of the int argument.

-----

Convert String to int

int j;    String str;

1.int j = Integer.parseInt(str);

      the integer value represented by the argument in decimal.

      Throws: NumberFormatException - if the string does not contain a parsable integer.
   

2.int j = Integer.valueOf(str);

      an Integer object holding the value represented by the string argument.

      Throws: NumberFormatException - if the string cannot be parsed as an integer.

留言

熱門文章