String类的转换功能:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
重点
char[] toCharArray()
String concat(String str)
public class StringDemo8 {
public static void main(String[] args) {
String s = "HelloWorLD";
//public byte[] getBytes()
// 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。
byte[] b1 = s.getBytes();
// System.out.println(b1); //[B@4554617c 数组没有重写toString()方法 打印的是地址值
for(int i=0;i<b1.length;i++){
System.out.println(b1[i]);
}
System.out.println("*********************");
//char[] toCharArray()将此字符串转换为新的字符数组。
// 字符串 ---> 字符数组
char[] chars = s.toCharArray();
for(int i = 0;i<chars.length;i++){
System.out.print(chars[i]);
}
System.out.println();
//增强for循环
// for (char aChar : chars) {
// System.out.println(aChar);
// }
System.out.println("*********************");
//字符数组 ----> 字符串
//static String valueOf(char[] chs)
System.out.println(chars);
System.out.println("*********************");
//static String valueOf(int i)
//将int类型的数据转化成字符串类型
String s1 = String.valueOf(100); // "100" --> 100
System.out.println(s1);
System.out.println("*********************");
//String toLowerCase()
//将字符串内容全部转化成小写
String s2 = s.toLowerCase(); //HelloWorLD
System.out.println(s2); //helloworld
System.out.println("*********************");
// String toUpperCase()
//将字符串内容全部转大写
String s3 = s.toUpperCase();//HelloWorLD
System.out.println(s3);//HELLOWORLD
System.out.println("*********************");
//public String concat(String str)将指定的字符串连接到该字符串的末尾。
//将小括号里面的字符串拼接到调用方法的字符串后面
String s4 = "Hello";
String s5 = "World";
String s6 = s4+s5;
String s7 = s5.concat(s6);
System.out.println("s4: "+s4); //Hello
System.out.println("s5: "+s5); //World
System.out.println("s6: "+s6); //HelloWorld
System.out.println("s7: "+s7); //WorldHelloWorld
// String s8 = "";
String s8 = null;
// System.out.println(s8.concat(s5));//NullPointerException
// System.out.println(s5.concat(s8));//NullPointerException
}
}