StringBuilder Class in Java

The StringBuilder class in Java provides a mutable sequence of characters, allowing for efficient string manipulation. It is commonly used when you need to dynamically modify or build strings. Here are some important details to consider:

Creating a StringBuilder object:

To create a StringBuilder object, you can use its default constructor or provide an initial capacity as a parameter.

The initial capacity represents the expected length of the StringBuilder, but it can dynamically grow as needed.

Example:


StringBuilder stringBuilder = new StringBuilder();

In this example, an empty StringBuilder object is created.


Appending and modifying strings:

The append() method is used to add or concatenate strings to the StringBuilder object.

It provides efficient string concatenation without creating intermediate string objects.

The append() method can accept various data types, including strings, characters, numbers, and other objects.

Example:


StringBuilder stringBuilder = new StringBuilder("Hello"); 
stringBuilder.append(", World!");
System.out.println(stringBuilder.toString()); // Output:

In this example, the append() method is used to concatenate ", World!" to the existing string in the StringBuilder object. 

The toString() method is then called to obtain the final modified string, which is "Hello, World!".


Inserting and deleting characters:

The insert() method allows you to insert characters or strings at a specific position in the StringBuilder.

The delete() method is used to remove characters or a range of characters from the StringBuilder object.

Example:


StringBuilder stringBuilder = new StringBuilder("Hello World");
stringBuilder.insert(5, ", "); 
System.out.println(stringBuilder.toString()); // Output: Hello, World
stringBuilder.delete(5, 8); 
System.out.println(stringBuilder.toString()); // Output: 

In this example, the insert() method is used to insert ", " at index 5, resulting in the modified string "Hello, World". The delete() method is then used to remove the characters from index 5 to 7, resulting in the final string "HelloWorld".

charAt() - Returns the character at the specified index position within the StringBuffer object.

Example:


StringBuffer stringBuffer = new StringBuffer("Hello");
char character = stringBuffer.charAt(2); 
System.out.println(character); // Output: l

indexOf() - Returns the index of the first occurrence of the specified substring within the StringBuffer object.

Example:


StringBuffer stringBuffer = new StringBuffer("Hello World"); 
int index = stringBuffer.indexOf("World");
 System.out.println(index); // Output: 6

lastIndexOf() - Returns the index of the last occurrence of the specified substring within the StringBuffer object.

Example:


StringBuffer stringBuffer = new StringBuffer("Hello World"); 
int lastIndex = stringBuffer.lastIndexOf("o");
 System.out.println(lastIndex); // Output: 7

substring() - Returns a new String object that contains a subsequence of characters from the StringBuffer object.

Example:


StringBuffer stringBuffer = new StringBuffer("Hello World"); 
String subString = stringBuffer.substring(6); 
System.out.println(subString); // Output: World

toString() - Converts the StringBuffer object to a String object.

Example:-


StringBuffer stringBuffer = new StringBuffer("Hello World");
String str = stringBuffer.toString();
 System.out.println(str); // Output: Hello World

capacity() - Returns the current capacity of the StringBuffer object.

Example:-


StringBuffer stringBuffer = new StringBuffer("Hello");
int capacity = stringBuffer.capacity(); 
System.out.println(capacity); // Output: 21

setCharAt() - Modifies the character at the specified index position within the StringBuffer object.

Example:


StringBuffer stringBuffer = new StringBuffer("Hello"); 
stringBuffer.setCharAt(1, 'a'); 
System.out.println(stringBuffer.toString()); // Output: Hal

Other useFul Methods:

The StringBuilder class provides other methods for string modification, such as replace(), reverse(), substring(), and more.

These methods allow you to perform a range of string manipulation operations efficiently.

Example:


StringBuilder stringBuilder = new StringBuilder("Hello World");
stringBuilder.replace(6, 11, "Java");
System.out.println(stringBuilder.toString()); // Output: Hello Java
stringBuilder.reverse(); 
System.out.println(stringBuilder.toString()); // Output: 

In this example, the replace() method is used to replace the substring "World" with "Java", resulting in the modified string "Hello Java". The reverse() method is then called to reverse the characters in the string.


By using the StringBuilder class in Java, you can efficiently manipulate strings by appending, inserting, deleting, replacing, and performing other modifications. The mutable nature of StringBuilder allows for dynamic string building without the need for multiple intermediate string objects, resulting in improved performance and reduced memory usage.