• 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);

    拼接字符串

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