- 使用append()方法来添加字符或字符串到StringBuilder对象中:
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
System.out.println(sb.toString());
- 使用insert()方法来在指定位置插入字符或字符串到StringBuilder对象中:
StringBuilder sb = new StringBuilder("Hello World");
sb.insert(5, ",");
System.out.println(sb.toString());
- 使用delete()方法来删除指定范围内的字符或字符串:
StringBuilder sb = new StringBuilder("Hello, World");
sb.delete(5, 7);
System.out.println(sb.toString());
- 使用replace()方法来替换指定范围内的字符或字符串:
StringBuilder sb = new StringBuilder("Hello, World");
sb.replace(7, 12, "Java");
System.out.println(sb.toString());
- 使用reverse()方法来反转StringBuilder对象中的字符序列:
StringBuilder sb = new StringBuilder("Hello");
sb.reverse();
System.out.println(sb.toString());