Java自学-String(2)

Java自学-String(2)

1、StringBuilder

Java自学-String(2)

2、StringBuilder构造方法

Java自学-String(2)

package lesson02;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/12 21:49
 * FileName: StringBuilderDemo01
 * Description:
 */
public class StringBuilderDemo01 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        System.out.println("sb:"+sb);
        System.out.println("sb.length():"+sb.length());

        StringBuilder sb2 = new StringBuilder("hello");
        System.out.println("sb2:"+sb2);
        System.out.println("sb2.length():"+sb2.length());
    }
}

3、StringBuilder添加和反转

Java自学-String(2)

package lesson02;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/12 21:53
 * FileName: StringBuilderDemo02
 * Description:
 */
public class StringBuilderDemo02 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

//        StringBuilder sb2 = sb.append("hello");
//
//        System.out.println("sb:"+sb);
//        System.out.println("sb2:"+sb2);
//        System.out.println(sb==sb2);

//        sb.append("hello");
//        sb.append("world");
//        sb.append("java");
//        sb.append(100);

        //链式编程
        sb.append("hello").append("world").append("java").append(100);
        System.out.println("sb:"+sb);

        sb.reverse();
        System.out.println("sb:"+sb);
    }
}

4、StringBuilder和String相互转换

Java自学-String(2)

package lesson02;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 6:14
 * FileName: StringBuilderDemo03
 * Description:
 */
public class StringBuilderDemo03 {
    public static void main(String[] args) {
//        StringBuilder sb = new StringBuilder();
//        sb.append("hello");
//
//        String s = sb.toString();
//        System.out.println(s);

        String s="hello";
        StringBuilder sb = new StringBuilder(s);
        System.out.println(sb);
    }
}

5、字符串拼接升级版

Java自学-String(2)

package lesson02;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 6:21
 * FileName: StringBuilderTest01
 * Description:
 */
public class StringBuilderTest01 {
    public static void main(String[] args) {
        int[] arr={1,2,3};
        String s = arraytoString(arr);

        System.out.println(s);
    }

    public static String arraytoString(int[] arr){
        StringBuilder sb = new StringBuilder();

        sb.append("[");

        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                sb.append(arr[i]);
            }else{
                sb.append(arr[i]).append(", ");
            }
        }
        sb.append("]");

        String s = sb.toString();
        return s;
    }
}

6、字符串反转升级版

Java自学-String(2)

package lesson02;

import java.util.Scanner;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 6:29
 * FileName: StringBuilderTest02
 * Description:
 */
public class StringBuilderTest02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串:");
        String s = sc.nextLine();

        String s1 = myReverse(s);
        System.out.println(s1);
    }

    public static String myReverse(String s){
//        StringBuilder sb = new StringBuilder(s);
//        sb.reverse();
//        String s1 = sb.toString();
//        return s1;
//
        return new StringBuilder(s).reverse().toString();
    }
}

7、帮助文档查看StringBuilder方法

Java自学-String(2)

上一篇:API


下一篇:String 、StringBuilder、 StringBuffer区别(面试题)