//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); } }
本文参考链接:https://www.cnblogs.com/wgl1995/p/9365552.html