StringBuilder and StringBuffer are two important classes in Java that are used to efficiently manipulate strings. While both classes are very similar, StringBuilder is preferred when the string is not accessed by multiple threads and StringBuffer is preferred when the string can be accessed by multiple threads.
There are some common methods that are present in all three classes i.e. String class, StringBuilder class, and StringBuffer class. These methods are:
- length(): This method returns the number of characters the StringBuilder object contains.
- charAt(index): This method is used to return the character at the specified index of the string contained by the StringBuilder object.
- indexOf(str): This method is used to return the index within the string for the first occurrence of the passed substring as the parameter.
- indexOf(str, fromIndex): This method is used to return the index within the string for the first occurrence of the passed substring as a parameter starting at the specified index ‘fromIndex’.
- lastIndexOf(str): This method is used to return the index within the string for the last occurrence of the passed substring as a parameter.
- lastIndexOf(str, fromIndex): This method is used to return the index of a substring within the original string, starting at index 0 and ending at the index fromIndex.
- compareTo(sb): This method compares two strings lexicographically.
- substring(beginIndex): This method is used to return a substring starting from beginIndex and extending to the end of the string.
- substring(beginIndex, lastIndex): This method is used to return a substring starting from beginIndex and extending to the index lastIndex-1 of the string.
- chars(): This method returns a stream of int zero-extending the char values from this sequence.
There are also some methods that are present only in the StringBuilder class and the StringBuffer class. These methods are:
- append(x): This method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used by passing various types of arguments. Here, the x can be of boolean, int, char, String, float, or Object type.
- insert(offset, x): This method inserts the string representation of the given data type at the given offset position in a StringBuffer. Here, the x can be of boolean, int, char, String, float, or Object type.
- setCharAt(index, c): This method is used to set the character at the position index passed as c.
- reverse(): This method is used to reverse the characters in the StringBuilder.
- deleteCharAt(index): This method removes the character at the given index from the string contained by StringBuilder.
- delete(start, end): This method removes the characters starting from index start to index end-1 from the string contained by StringBuilder.
- capacity(): This method is used to return the current capacity of the StringBuilder object.
- replace(start, end, str): This method is used to replace the characters in a substring of this sequence with characters in the specified string.
Here's an example Java code to illustrate the internal working of StringBuilder and StringBuffer class:
csharppublic class StringBuilderAndStringBufferExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("dcba");
// Reversing the StringBuilder
sb.reverse();
System.out.println("Reversed StringBuilder: " + sb);
// Appending to sb
sb.append("efg");
System.out.println("StringBuilder after append: " + sb);
// Replacing the character at index 1 with 'h'
sb.setCharAt(1, 'h');
System.out.println("StringBuilder after replacing char at index 1: " + sb);
// Delete the characters at index 0 to 1
sb.delete(0, 2);
System.out.println("StringBuilder after delete: " + sb);
// Inserts "efg" at index 1
sb.insert(1, "efg");
System.out.println("StringBuilder after insert: " + sb);
// Create a StringBuffer object
StringBuffer sBuffer = new StringBuffer("abcd");
// Reversing the StringBuffer
sBuffer.reverse();
System.out.println("Reversed StringBuffer: " + sBuffer);
// Appending to sBuffer
sBuffer.append("efg");
System.out.println("StringBuffer after append: " + sBuffer);
// Replacing the character at index 1 with 'h'
sBuffer.setCharAt(1, 'h');
System.out.println("StringBuffer after replacing char at index 1: " + sBuffer);
// Delete the characters at index 0 to 1
sBuffer.delete(0, 2);
System.out.println("StringBuffer after delete: " + sBuffer);
// Inserts "efg" at index 1
sBuffer.insert(1, "efg");
System.out.println("StringBuffer after insert: " + sBuffer);
}
}
In this version, I have added comments to describe each step of the program. I have also added a section to illustrate the same operations with a StringBuffer object, so the user can see the difference between StringBuilder and StringBuffer. I hope this helps improve your understanding! Let me know if you have any other questions.