Java StringBuilder Methods

The StringBuilder class in Java is used to create mutable (modifiable) string objects. Unlike the String class, which is immutable, the StringBuilder class provides an array of methods that allow for the modification of strings without the need to create new string objects.

This guide covers various methods available in the StringBuilder class. Each method is described in simple terms to help beginners understand how to use them. These methods enable dynamic modification and manipulation of string data, making it a powerful tool in Java programming.

Java StringBuilder Methods

The table below contains various methods of the Java StringBuilder class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
append() Appends the specified string to this character sequence.
capacity() Returns the current capacity of the StringBuilder.
charAt() Returns the character at the specified index.
chars() Returns a stream of int values representing the characters in this sequence.
codePointAt() Returns the Unicode code point at the specified index.
delete() Removes the characters in a substring of this sequence.
deleteCharAt() Removes the character at the specified index.
ensureCapacity() Ensures that the capacity is at least equal to the specified minimum.
getChars() Copies characters from this sequence into the destination character array.
indexOf() Returns the index within this string of the first occurrence of the specified substring.
insert() Inserts the specified string into this sequence at the specified position.
lastIndexOf() Returns the index within this string of the last occurrence of the specified substring.
length() Returns the length (character count) of this sequence.
repeat() Returns a string whose value is the concatenation of this string repeated the specified number of times.
replace() Replaces the characters in a substring of this sequence with characters in the specified string.
reverse() Causes this character sequence to be replaced by the reverse of the sequence.
subSequence() Returns a new character sequence that is a subsequence of this sequence.
substring() Returns a new string that is a substring of this sequence.
trimToSize() Attempts to reduce storage used for the character sequence.

The StringBuilder class is part of the java.lang package. It provides essential methods for creating and manipulating mutable strings, making it easier to handle and modify string data efficiently in Java programming.

For more detailed information, please refer to the official Java SE Documentation.

Leave a Comment

Scroll to Top