StringBuilder
The StringBuilder
class in Java represents a String
-like character sequence that is mutable, whereas objects of the String
class are immutable. This provides an alternative to the String
class when it’s a requirement to change a character sequence once it is defined.
It is similar to another class, StringBuffer
, but it is faster in most circumstances. The difference between StringBuilder
and StringBuffer
is that StringBuilder
is not thread-safe. StringBuilder
offers no guarantee of synchronization and should not be used across multiple threads.
Syntax
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder(capacity);
StringBuilder s3 = new StringBuilder(sequence);
A StringBuilder
can be created in the following ways:
- Calling the constructor with no arguments creates a
StringBuilder
with no contents and a capacity of 16 characters. - Calling the constructor with the
int
argument,capacity
, creates aStringBuilder
with no contents and a capacity ofcapacity
characters. - Calling the constructor with a
sequence
argument that is either aString
or aCharSequence
creates aStringBuilder
with contents and capacity equal to the specifiedString
orCharSequence
.
Example
The following example creates a StringBuilder
with a specified String
and then changes it by appending another String
:
import java.util.*;public class Example {public static void main(String[] args){StringBuilder str = new StringBuilder("Hello");System.out.println(str.toString());str.append(" World!");System.out.println(str.toString());}}
This produces the following output:
HelloHello World!
Below is a list of methods used by the StringBuilder
class:
StringBuilder
- .append()
- Appends the string representation of its argument.
- .capacity()
- Returns the current space available for characters in the StringBuilder.
- .delete()
- Removes a substring from the contents of a StringBuilder and returns the modified object.
- .deleteCharAt()
- Removes the character at the specified index from the contents of a StringBuilder and returns the modified object.
- .indexOf()
- Returns the index of the first occurrence of a substring in the StringBuilder or -1 if none are found.
- .insert()
- Places a sequence of characters into a StringBuilder and returns a reference to the object.
- .lastIndexOf()
- Returns the index of the last (rightmost) occurrence of a substring in the StringBuilder or -1 if no substring is found.
- .length()
- Returns the number of characters currently in the StringBuilder.
- .replace()
- Switches a substring in a StringBuilder with a specified String and returns the modified object.
- .reverse()
- Returns a modified StringBuilder object with its character sequence rearranged in the opposite order.
- .substring()
- Returns a String object representing a substring of the character sequence currently in a given StringBuilder.
- .toString()
- Returns a String representation of the character sequence currently in a given StringBuilder.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours