字符串和字符数组的转换
字符串和字符数组存在以下图的转换关系:
byte[]转String
byte[] b1 = {99,100,101,102,103,104,105};
String s1 = new String(b1); //转换为ASCLL对应的字母
String转byte[]
String s2 = "a数据类型转换";
byte[] b2 = s2.getBytes(); //转换为对应的GBK编码(默认)
System.out.println(Arrays.toString(s2.getBytes()));
char[]转String
char[] ch = {'h','e','l','l','o','w','o','r','l','d'};
String str = new String(ch);
System.out.println(str);
输出如:
String转char[]
String str = "HelloWorld";
char[] ch = str.toCharArray();
System.out.println(Arrays.toString(ch));
看一下输出: