- byte[] getBytes();
把字符串转换为字节数组
- char[] toCharArray();
把字符串转换为字符数组
- static String valueOf(char[] chs);
把字符数组转成字符串
- static String valueOf(int i);
把int类型的数据转成字符串
注意:String类的valueOf()方法可以把任意类型的数据转成字符串.
- String toLowerCase();
把字符串转成小写
- String toUpperCase();
把字符串转成大写
- String concat(String str);
拼接字符串
private static void demo1() throws UnsupportedEncodingException {String s1 = "abc";byte[] bytes = s1.getBytes();for (int i = 0; i < bytes.length; i++) {System.out.println(bytes[i]);}/*979899*/String s2 = "中华人民共和国";//编码:把我们认识的字符串,转换为计算机可以识别的字节byte[] bytes1 = s2.getBytes("gbk");/*GBK码表的特点:一个中文代表两个字节中文的第一个字节是负数*/for (int i = 0; i < bytes1.length; i++) {System.out.println(bytes1[i]);}}
private static void demo2() {//把字符串转换为字符数组String s = new String("hello world");char[] chars = s.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.println(chars[i]);}}
private static void demo3() {String s = String.valueOf(97);System.out.println(s);String s1 = String.valueOf(3.1415926);System.out.println(s1);String s2 = String.valueOf('a');System.out.println(s2);char[] arr = {'a','b','c'};String s3 = String.valueOf(arr);System.out.println(s3);Student student = new Student("ali", 18);String s4 = String.valueOf(student);System.out.println(s4);System.out.println(student.toString());}
public static void main(String[] args) throws UnsupportedEncodingException {//字符串大小写转换String s = "hello WORLD";String s1 = s.toLowerCase();String s2 = s.toUpperCase();System.out.println(s);//hello WORLDSystem.out.println(s1);//hello worldSystem.out.println(s2);//HELLO WORLD//字符串拼接String s3 = "hello";String s4 = "world";System.out.println(s3 + s4);//helloworldSystem.out.println(s3.concat(s4));//helloworld/*用+号拼接更强大,可以用字符串与任意类型拼接concat方法只能字符串类型的对象才能调用*/}
