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
str
to the end of the currentStringBuilder
object. - Returns the modified
StringBuilder
object.
- Appends the specified string
-
insert(int offset, String str)
:- Inserts the specified string
str
at the givenoffset
position in the currentStringBuilder
object. - Returns the modified
StringBuilder
object.
- Inserts the specified string
-
delete(int start, int end)
:- Removes the characters from the
start
index (inclusive) to theend
index (exclusive) from the currentStringBuilder
object. - Returns the modified
StringBuilder
object.
- Removes the characters from the
-
deleteCharAt(int index)
:- Removes the character at the specified
index
from the currentStringBuilder
object. - Returns the modified
StringBuilder
object.
- Removes the character at the specified
-
replace(int start, int end, String str)
:- Replaces the characters from the
start
index (inclusive) to theend
index (exclusive) with the specified stringstr
. - Returns the modified
StringBuilder
object.
- Replaces the characters from the
-
reverse()
:- Reverses the characters in the current
StringBuilder
object. - Returns the modified
StringBuilder
object.
- Reverses the characters in the current
-
substring(int start)
andsubstring(int start, int end)
:- These methods are similar to the corresponding methods in the
String
class. - The first variant returns the substring starting from the
start
index to the end of theStringBuilder
. - The second variant returns the substring from the
start
index (inclusive) to theend
index (exclusive).
- These methods are similar to the corresponding methods in the
-
length()
:- Returns the length (number of characters) of the
StringBuilder
object.
- Returns the length (number of characters) of the
-
capacity()
:- Returns the current capacity (size) of the
StringBuilder
object.
- Returns the current capacity (size) of the
-
setLength(int newLength)
:
-
- Sets the length of the
StringBuilder
to the specifiednewLength
. - If
newLength
is greater than the current length, theStringBuilder
will be padded with null characters ('\0') to reach the desired length. - If
newLength
is smaller than the current length, characters beyond thenewLength
will 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 specifiedindex
to 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.