package cn.itcast_05;
/*
- String类的转换功能:
byte[] getByte():把字符串转换成字节数组
- 复习:
public String(byte[] bytes):把字节数组转换成字符串
char[] toCharArray():把字符串转换成字符数组
static String valueOf(char[] chs):把字符数组转换成字符串
static String valueOf(int i):把int类型的数据转成字符串
- 注意:
String类的valueOf()可以把任意类型的数据转换成字符串
String toLowerCase():把字符串转成小写
String toUpperCase():把字符串转成大写
String concat(String str):把字符串拼接
*/
public class StringDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义一个字符串对象
String s = "JavaSE";
//byte[] getByte():把字符串转换成字节数组
byte[] bys = s.getBytes();
for(int x=0; x<s.length(); x++) {
System.out.println(bys[x]);//输出的是数字:74,97,118,97,83,69
System.out.println("---------------------------------------------------");
}
//复习:
//public String(byte[] bytes):把字节数组转换成字符串
String str = new String(bys);
System.out.println("str:" + str);//JavaSE
System.out.println("---------------------------------------------------");
//char[] toCharArray():把字符串转换成字符数组
char[] chs = s.toCharArray();
for(int x=0; x<s.length(); x++) {
System.out.println(chs[x]);//输出的是字符:J,a,v,a,S,E
}
System.out.println("---------------------------------------------------");
//static String valueOf(char[] chs):把字符数组转换成字符串
String ss = String.valueOf(chs);//string是静态类型,所以可以直接用类调用
System.out.println(ss);//JavaSE
System.out.println("---------------------------------------------------");
//static String valueOf(int i):把int类型的数据转成字符串
int i = 100;
System.out.println(i);//字符串类型:100
System.out.println("---------------------------------------------------");
//String toLowerCase():把字符串转成小写
System.out.println(s.toLowerCase());//javase
System.out.println("---------------------------------------------------");
//String toUpperCase():把字符串转成大写
System.out.println(s.toUpperCase());//JAVASE
System.out.println("---------------------------------------------------");
//String concat(String str):把字符串拼接
String str3 = s.concat(ss);
System.out.println(str3);//JavaSEJavaSE
System.out.println("---------------------------------------------------");
String str4 = s + ss;
System.out.println(str4);//JavaSEJavaSE
System.out.println("---------------------------------------------------");
}
}