StringBuilder in Java | Constructors, Methods, and Examples

StringBuilder in Java is a class used to create a mutable, or in other words, a modifiable succession of characters. Like StringBuffer, the StringBuilder class is an alternative to the Java Strings Class, as the Strings class provides an immutable succession of characters. However, there is one significant difference between StringBuffer and StringBuilder, and it is that the latter is non-synchronized. It means that StringBuilder in Java is a more suited choice while working with a single thread, as it will be quicker than StringBuffer.

Class Declaration of StringBuilder

The java.lang.StringBuilder class is a part of java.lang package and has the following class declaration:

public final class StringBuilder

extends Object

implements Serializable, CharSequence

Looking at the Constructors of StringBuilder in Java

The following table lists and describes the constructors of StringBuilder in Java

Constructor Name

Description

StringBuilder()

It constructs a blank string builder with a capacity of 16 characters

StringBuilder(int capacity)

It creates an empty string builder with the specified capacity

StringBuilder(CharSequence seq)

It creates a string builder with the same characters specified as the argument

StringBuilder(String str)

It will construct a string builder with the string specified in the argument

Since you now know about the constructors and class declaration of StringBuilder in Java, it’s time to look at an example where you will use some of these constructors to create various sequences of characters.

ConstructorsofStringBuilder

ConstructorsofStringBuilder_rslt

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Discussing Various Methods of StringBuilder in Java

The StringBuilder in Java provides numerous methods to perform different operations on the string builder. The tabledepicted below enlists some primary methods from the StringBuilder class.

Method

Description

StringBuilder append (String s)

This method appends the mentioned string with the existing string. You can also with arguments like boolean, char, int, double, float, etc.

StringBuilder insert (int offset, String s)

It will insert the mentioned string to the other string from the specified offset position. Like append, you can overload this method with arguments like (int, boolean), (int, int), (int, char), (int, double), (int, float), etc.

StringBuilder replace(int start, int end, String s)

It will replace the original string with the specified string from the start index till the end index.

StringBuilder delete(int start, int end)

This method will delete the string from the mentioned start index till the end index.

StringBuilder reverse()

It will reverse the string.

int capacity()

This will show the current StringBuilder capacity.

void ensureCapacity(int min)

This method ensures that the StringBuilder capacity is at least equal to the mentioned minimum.

char charAt(int index)

It will return the character at the specified index.

int length()

This method is used to return the length (total characters) of the string.

String substring(int start)

Starting from the specified index till the end, this method will return the substring.

String substring(int start, int end)

It will return the substring from the start index till the end index.

int indexOf(String str)

This method will return the index where the first instance of the specified string occurs.

int lastIndexOf(String str)

It will return the index where the specified string occurs the last.

Void trimToSize()

It will attempt to reduce the size of the StringBuilder.

Using the Methods of StringBuilder in Java

Let’s have a look at examples of some examples of the StringBuilder methods.

Example 1: Applying the Append() Method of StringBuilder in Java

Here, you must concatenate three strings using the append() method in the below example.

StringBuilderEx1

StringBuilderEx1_rslt

Example 2: Inserting String With the Insert() Method

In this example, you will insert one string into another at the specified index.

StringBuilderEx2

StringBuilderEx2_rslt

Example 3: Using the Replace() Method of StringBuilder in Java

You will use the replace() method to edit Simplilearn, and insert Java from a specified start and end index.

StringBuilderEx3

StringBuilderEx3_rslt

Example 4: Deleting a Substring From the Original String

The delete() method in the below example will delete some strings according to the specified indexes.

StringBuilderEx4.

StringBuilderEx4_rslt.

Example 5: Applying the Reverse() Method of StringBuilder in Java

You will reverse “Simplilearn” in the example below with the reverse() method.

StringBuilderEx5

StringBuilderEx5_rslt

Example 6: Looking at the Capacity() Method

You will find out the current capacity of a StringBuilder using the capacity() method. The default capacity is 16. When the number of characters exceeds 16, it increases the capacity to n*2+2, where n is the current capacity. Let’s look at the example.

/StringBuilderEx6

StringBuilderEx6_rsll

Example 7: Ensure Minimum Capacity With the Ensurecapacity() Method

In this example, you will use the ensureCapacity() method of StringBuilder in Java to ensure that there is the minimum capacity before proceeding with other operations.

StringBuilderEx7

/StringBuilderEx7_rslt.

Example 8: Using the Length() Method of Stringbuilder in Java

In this example, you will use the length() method to find the total number of characters in a string.

StringBuilderEx8

StringBuilderEx8_rslt.

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Example 9: Looking at the Charat() Method

Using the charAt() method, you will find the character present at the specified index in the string.

StringBuilderEx9.

StringBuilderEx9_rsllt.

Example 10: The Indexof() Method of StringBuilder in Java

Next, use the indexOf() method to find the index of the specified string from the original string.

StringBuilderEx10.

StringBuilderEx10_rslt

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

In this StringBuilder for Java article, you have learned everything about StringBuilder. You have also looked into the constructors and methods of the StringBuilder class. For more such basic concepts, you can refer to Simplilearn’s Java Tutorial for Beginners guide. But if you want to go advanced, opt for the Online Java Certification Course. The course offers a vast array of learning materials and applied learning, to help you grasp both basic and advanced concepts of Java programming.

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, right away!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.