6.3.3字符串常见功能:转换 1) 将字符串转成字节数组 public byte[] getBytes() 此功能目前用不到,什么时候使用,在IO流中会用 代码样式: Strings1 = “23”; byte[]b = s1.getBytes(); for(int x=0;x<b.length;x++){ System.out.println(b[x]); } 2) 将字符串转成字符数组 public char[] toCharArray() 将字符数组转成字符串怎么转 构造方法 new String(char[] ch) 代码样式: Strings1 = “asdfgg”; char[]ch = s1.toCharArray(); for(intx=0;x<ch.length;x++){ System.out.println(ch[x]+” ”); } //打印结果:as d f g g 3) 将字符串进行大小写转换 原始字符串不变 public StringtoUpperCase()转成大写 publicString toLowerCase()转成小写 代码样式: Strings1 = “asdad”; System.out.println(s1.toUpperCase()); //转换大写 System.out.println(s1.toLowerCase()); //转换小写 4) 将字符数组转成字符串 public static String copyValueOf(char[] ch) 代码样式: char[] ch11 ={'2','3','d'}; Strings12 = String.copyValueOf(ch11); System.out.println(s12); 5) 将基本数据类型转成字符串 int long float double charboolean 对象 public static String valueOf(基本类型) 代码样式: inti = 50; 将整数类型转换成字符串 Strings1 = String.valueOf(i); System.out.println(s1); double d = 50.22; 将double类型转换成字符串 Stringd1 = String.valueOf(d); System.out.println(d1); public static String valueOf(char[] ch) 将字符数组转换成字符串 char[] c1 = {'1','4','g'}; Stringc2 = String.valueOf(c1); System.out.println(c2);
|