Class relations: + java.lang.Object + - java.lang.StringBuilder
Implemented interfaces: Serializable, Appendable, CharSequence
Thread-safe: False
Similar to StringBuffer, StringBuilder is a class that represent a mutable sequence of characters, but with no guarantee of synchronisation and is not thread-safe. It is designed for use in single-threaded context, offering better performance than StringBuffer due to the lack of synchronisation overhead.
hi
StringBuilder constructors
Similar to StringBuffer, StringBuilder has three constructors for different needs.
StringBuilder()
- The default constructor creates an empty StringBuilder with an initial capacity of 16.
StringBuilder(String str)
- Creates a StringBuilder with the specified string.
StringBuilder(int capacity)
- Creates an empty StringBuilder with the specified capacity.
StringBuilder methods
StringBuilder has similar methods to StringBuffer.
Method | Description | Ref |
---|---|---|
int capacity() | It returns the current capacity | |
int length() | It returns the length (character count) | |
StringBuilder append(String s) | It appends the specific string to the calling string. This is an overloaded method to support other types | |
StringBuilder insert(int offset, String s) | It inserts the specific string to a specific position in the calling string. This is an overloaded method to support other types | |
StringBuilder replace(int startIndex, int endIndex, String str) | It replaces the string from specified startIndex and endIndex with the specific string | |
StringBuilder delete(int startIndex, int endIndex) | It deletes the string from specified startIndex and endIndex | |
StringBuilder deleteCharAt(int index) | It removes the char at the specified position | |
void setCharAt(int index, char ch) | It sets the character at the specified index to ch | |
StringBuilder reverse() | It reverses the string | |
void ensureCapacity(int minimumCapacity) | It is used to ensure the capacity at least equal to the given minimum, useful when the approximate size of the buffer in known in advance, to avoid frequency of reallocations | |
char charAt(int index) | It returns char value for the particular index | |
String substring(int beginIndex) | It returns a substring for given begin index | String Class |
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_DevelopmentProgramming_LanguagesJavaStringStringBuilder