QUESTIONS & ANSWERS
JAVA
Explain all important methods of StringBuilder class?
StringBuilder is similar to StringBuffer in functionality but is not thread-safe, making it more efficient in single-threaded environments. It provides several methods to manipulate and modify strings efficiently. Here are some of the important methods in the StringBuilder class:
-
append(String str):- Appends the specified string
strto the end of the currentStringBuilderobject. - Returns the modified
StringBuilderobject.
- Appends the specified string
-
insert(int offset, String str):- Inserts the specified string
strat the givenoffsetposition in the currentStringBuilderobject. - Returns the modified
StringBuilderobject.
- Inserts the specified string
-
delete(int start, int end):- Removes the characters from the
startindex (inclusive) to theendindex (exclusive) from the currentStringBuilderobject. - Returns the modified
StringBuilderobject.
- Removes the characters from the
-
deleteCharAt(int index):- Removes the character at the specified
indexfrom the currentStringBuilderobject. - Returns the modified
StringBuilderobject.
- Removes the character at the specified
-
replace(int start, int end, String str):- Replaces the characters from the
startindex (inclusive) to theendindex (exclusive) with the specified stringstr. - Returns the modified
StringBuilderobject.
- Replaces the characters from the
-
reverse():- Reverses the characters in the current
StringBuilderobject. - Returns the modified
StringBuilderobject.
- Reverses the characters in the current
-
substring(int start)andsubstring(int start, int end):- These methods are similar to the corresponding methods in the
Stringclass. - The first variant returns the substring starting from the
startindex to the end of theStringBuilder. - The second variant returns the substring from the
startindex (inclusive) to theendindex (exclusive).
- These methods are similar to the corresponding methods in the
-
length():- Returns the length (number of characters) of the
StringBuilderobject.
- Returns the length (number of characters) of the
-
capacity():- Returns the current capacity (size) of the
StringBuilderobject.
- Returns the current capacity (size) of the
-
setLength(int newLength):
-
- Sets the length of the
StringBuilderto the specifiednewLength. - If
newLengthis greater than the current length, theStringBuilderwill be padded with null characters ('\0') to reach the desired length. - If
newLengthis smaller than the current length, characters beyond thenewLengthwill be truncated.
- Sets the length of the
charAt(int index)andsetCharAt(int index, char ch):
-
charAt(int index)returns the character at the specifiedindex.setCharAt(int index, char ch)sets the character at the specifiedindexto the givench.
These methods provide a wide range of string manipulation functionalities, making StringBuilder a powerful tool for building and modifying strings efficiently in Java. Remember that StringBuilder is mutable, so you can modify its contents and perform various string operations without creating multiple objects.