StringBuffer
class in Java provides a way to manipulate strings. The insert()
method is used to insert another string or character sequence into a StringBuffer
object at a specified position.insert()
method to insert a string into a StringBuffer
object at index 0.StringBuffer sb = new StringBuffer("Hello");
sb.insert(0, "World, ");
System.out.println(sb.toString()); // Output: "World, Hello"
insert()
method to insert a character sequence into a StringBuffer
object at index 5.StringBuffer sb = new StringBuffer("Hello");
sb.insert(5, ", World");
System.out.println(sb.toString()); // Output: "Hello, World"
insert()
method to insert a character into a StringBuffer
object at index 2.StringBuffer sb = new StringBuffer("abc");
sb.insert(2, 'd');
System.out.println(sb.toString()); // Output: "abd"
StringBuffer
class is part of the java.lang
package in Java's standard library.