阅读量:0
- 使用append()方法来添加字符或字符串到StringBuilder对象中:
StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("World"); System.out.println(sb.toString()); // Output: Hello World
- 使用insert()方法来在指定位置插入字符或字符串到StringBuilder对象中:
StringBuilder sb = new StringBuilder("Hello World"); sb.insert(5, ","); // Insert a comma at index 5 System.out.println(sb.toString()); // Output: Hello, World
- 使用delete()方法来删除指定范围内的字符或字符串:
StringBuilder sb = new StringBuilder("Hello, World"); sb.delete(5, 7); // Delete characters from index 5 to 7 System.out.println(sb.toString()); // Output: Hello World
- 使用replace()方法来替换指定范围内的字符或字符串:
StringBuilder sb = new StringBuilder("Hello, World"); sb.replace(7, 12, "Java"); // Replace characters from index 7 to 12 with "Java" System.out.println(sb.toString()); // Output: Hello, Java
- 使用reverse()方法来反转StringBuilder对象中的字符序列:
StringBuilder sb = new StringBuilder("Hello"); sb.reverse(); System.out.println(sb.toString()); // Output: olleH