StringBuilder基本用法

//StringBuilder用法
public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        //追加字符串
        sb.append("Java");
        //sb = "Java";
        System.out.println(sb);
        //插入
        sb.insert(0, "Hello ");
        //sb = "Hello Java";
        System.out.println(sb);
        //替换
        sb.replace(5, 6, ",");
        //sb = "Hello,Java";
        System.out.println(sb);
        //删除
        sb.delete(5, 6);
        //sb = "HelloJava";
        System.out.println(sb);
        //反转
        sb.reverse();
        System.out.println(sb);
        //输出9
        System.out.println(sb.length());
        //输出16
        System.out.println(sb.capacity());
        //改变StringBuilder的长度,只保留前面部分
        sb.setLength(5);
        //sb = "avaJo";
        System.out.println(sb);
    }
}

 

 

posted @ 2018-07-25 13:51  王甘林  阅读(483)  评论(0编辑  收藏  举报