字符串与数值、字符数组之间的转换

来源:互联网 发布:日本女朋友体验 知乎 编辑:程序博客网 时间:2024/05/16 18:45

字符串数值的转换方法 :

字符串→数值

方法1: √parse方法常用

int i = Integer.parseInt("123");

double d = Double.parseDouble("1.23");

方法2

int i =Integer.valueOf("123").intValue();

 

注意:字符串转换成数值时对数据格式要求严格

int i = Integer.parseInt("123.4"); ×

int I = Integer.valueOf("123.4").intValue(); ×

 

数值→字符串

方法1

String s=String.valueOf(value);

其中value为任一种数字类型。

方法2

String s = Integer.toString(123);   

方法3:最直接

String s = "" + value;  其中value为任意一种数字类型。

 

字符数组字符串的转换方法:

字符数组→字符串:

    例如: char[ ] c={'a','b','c'};

           String str=new String(c);

 

字符串→字符数组:

    例如: String str="abc";

           char[ ]  c=str.toCharArray();

           System.out.print(c[1]);

0 0