String类的转换功能:
* 注意:下面的所有方法,都不会对原字符串产生变化
*
* byte[] getBytes():将一个String转换为一个byte[]数组
char[] toCharArray():将本字符串转换为char[]数组;
static String valueOf(char[] chs):静态方法。将字符数组转换为String
static String valueOf(int i):静态方法。将一个int值转换为一个String;
String toLowerCase():全部转换为小写
String toUpperCase():全部转换为大写
String concat(String str):将参数字符串追加到当前字符串的末尾。相当于+符号的字符串相连;
*/
public class Demo {
public static void main(String[] args) {
String s1 = "abc";
s1 = "你好";
byte[] byteArray = s1.getBytes();//使用GBK
for(int i = 0;i < byteArray.length ; i++){
System.out.println(byteArray[i]);
}
System.out.println("------------------------------");
s1 = "你好java";
char[] charArray = s1.toCharArray();
for(int i = 0 ;i < charArray.length ; i++){
System.out.println(charArray[i]);
}
charArray = new char[]{'a','b','你','好'};
String s2 = String.valueOf(charArray);
System.out.println("s2 = " + s2);
int n = 200;
String s3 = String.valueOf(n);
String s4 = "" + n;
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
String s5 = "Hello";
// s5 = "你好";
System.out.println("全部转换为大写:" + s5.toUpperCase());
System.out.println("全部转换为小写:" + s5.toLowerCase());
System.out.println("原字符串s5 = " + s5);
String s6 = "Hello";
String s7 = "World";
System.out.println(s6.concat(s7));
}
}
|
|