StringBuilder Class
The StringBuilder class in Java is used to represent a mutable sequence of characters. It is similar to the StringBuffer class, but it is not thread-safe, which means that it should not be used in multithreaded applications where multiple threads may access the same instance of the StringBuilder object. The class provides a number of useful methods to perform various operations on strings. Here are some common methods of the StringBuilder class:
append(String str): Appends the specified string to the end of the buffer.
insert(int offset, String str): Inserts the specified string into the buffer at the specified offset.
delete(int start, int end): Deletes the characters in the buffer between the start and end indices.
reverse(): Reverses the characters in the buffer.
capacity(): Returns the current capacity of the buffer.
ensureCapacity(int minimumCapacity): Ensures that the buffer has at least the specified minimum capacity.
length(): Returns the length of the buffer.
setCharAt(int index, char ch): Sets the character at the specified index to the specified value.
substring(int start, int end): Returns a new string that is a substring of the buffer.
toString(): Returns the string representation of the buffer.
These methods can be used to manipulate and extract information from string builders in various ways. Some methods are used for simple operations such as appending or deleting characters from the buffer, while others are used for more complex operations such as reversing the order of the characters in the buffer or extracting a substring from the buffer.
StringBuilder class in Java Example
public class StringBuilderExample { public static void main(String[] args) { // Create a StringBuilder object StringBuilder sb = new StringBuilder("Hello"); System.out.println("StringBuilder is: " + sb); // Append a string to the StringBuilder sb.append(" World"); System.out.println("Appended StringBuilder is: " + sb); // Insert a string at a particular position in the StringBuilder sb.insert(5, ", "); System.out.println("Inserted StringBuilder is: " + sb); // Replace a substring in the StringBuilder with another substring sb.replace(6, 11, "Java"); System.out.println("Replaced StringBuilder is: " + sb); // Delete a substring from the StringBuilder sb.delete(0, 6); System.out.println("Deleted StringBuilder is: " + sb); // Reverse the order of the characters in the StringBuilder sb.reverse(); System.out.println("Reversed StringBuilder is: " + sb); } }
In this example, we create a StringBuilder object with the value "Hello". We then demonstrate how to append a string to the StringBuilder using append(), insert a string at a particular position in the StringBuilder using insert(), replace a substring in the StringBuilder with another substring using replace(), delete a substring from the StringBuilder using delete(), and reverse the order of the characters in the StringBuilder using reverse(). Finally, we print out the results to the console. Note that the StringBuilder class is similar to the StringBuffer class, but it is not synchronized and is therefore more efficient when used in a single-threaded environment.