«Back to Home

Core Java

Topics

StringBuilder Class In Java

StringBuilder Class
 
StringBuilder class is also used to create mutable string in Java. StringBuilder class is available since JDK 1.5.
 
StringBuilder class is similar as StringBuffer class except that it is non-synchronized, which means that it is not thread safe due to which all its methods are also non-synchronised in Java.
 
In other words, we can say that StringBuilder class is mutable sequence of characters, which provides an API compatible with StringBuffer, but it is not thread safe.
 
Three important constructors of StringBuilder class are,
  1. StringBuilder()- This constructor is creates a blank string builder with the initial capacity of 16.

  2. StringBuilder(String str)- This constructor creates a string builder with the particular string.

  3. StringBuilder(int length)- This constructor creates an blank string builder with the specified capacity as length.
There are many important methods of StringBuilder class in Java, which are,
 
public StringBuilder append(String s)

This method is used to append the particular string with this string and the append() method is overloaded like append(int), append(double) etc.
 
public StringBuilder insert(int offset, String s)

This method is used to insert the particular string with this string at the specified position and the insert() method is overloaded like insert(int, int), insert(int, float) etc.
 
public StringBuilder replace(int startIndex, int endIndex, String str)

This method is used to replace the string from the particular startIndex and endIndex.
 
public StringBuilder delete(int startIndex, int endIndex)

This method is used to delete the string from the particular startIndex and endIndex.
 
public StringBuilder reverse()

This method is used to reverse the string.
 
public int capacity()

This method is used to return the current capacity of the string.
 
public void ensureCapacity(int minimumCapacity)

This method is used to ensure the capacity, at least equal to the given minimum.
 
public char charAt(int index)

This method is used to return the character at the specified position.
 
public String substring(int beginIndex)

This method is used to return the substring from the particular beginIndex.
 
public String substring(int beginIndex, int endIndex)

This method is used to return the substring from the particular beginIndex and endIndex.
 
There are many methods of StringBuilder class which methods are also non-synchronised in Java.
 
Let's see the examples of methods of StringBuilder class.
 
StringBuilder append() method

The append() method is used to concatenates the given argument with the string.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder n = new StringBuilder("Mariya ");  
  4.         n.append("Jones");  
  5.         System.out.println(n);  
  6.     }  
  7. }  
1

Output

2

In the above example, Original string is changed and gives the output.
 
StringBuilder insert() method

The insert() method is used to insert the given string with the string at the specified position.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder n = new StringBuilder("Sophia ");  
  4.         n.insert(3"Noah");  
  5.         System.out.println(n);  
  6.     }  
  7. }  
3
 
Output

4

In the above example, Original string is changed and gives the output.
 
StringBuilder replace() method

The replace() method is used to replace the given string from the particular beginIndex and endIndex.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder n = new StringBuilder("William");  
  4.         StringBuilder n1 = new StringBuilder("Daniel");  
  5.         n.replace(12"James");  
  6.         n1.replace(12"James");  
  7.         System.out.println(n);  
  8.         System.out.println(n1);  
  9.     }  
  10. }  
5

Output

6

StringBuilder delete() method

The delete() method is used to delete the string from the particular beginIndex to endIndex.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder n = new StringBuilder("Harry ");  
  4.         StringBuilder n1 = new StringBuilder("Tommy ");  
  5.         n.delete(12);  
  6.         n1.delete(14);  
  7.         System.out.println(n);  
  8.         System.out.println(n1);  
  9.     }  
  10. }  
7
 
Output

8

StringBuilder reverse() method

The reverse() method is used to reverse the current string.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder n = new StringBuilder("Peter");  
  4.         n.reverse();  
  5.         System.out.println(n);  
  6.     }  
  7. }  
9
Output

10
 
StringBuilder capacity() method

The capacity() method is used to returns the current capacity of the Builder in Java. The default capacity of the Builder is 16 and If the number of character increases from its current capacity then, it increases the capacity by (old capacity*2) +2.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder c = new StringBuilder();  
  4.         System.out.println(c.capacity());  
  5.         c.append("William Shakespeare");  
  6.         System.out.println(c.capacity());  
  7.         c.append("William Shakespeare is a famous writer");  
  8.         System.out.println(c.capacity());  
  9.     }  
  10. }  
11
 
Output

12

StringBuilder ensureCapacity() method

The ensureCapacity() method is used to ensures that the given capacity is the minimum to the current capacity and If it is greater than the current capacity then, it increases the capacity by (old capacity*2)+2.
 
Let’s see an example.
 
Code
  1. public class StringBuilderMethods {  
  2.     public static void main(String args[]) {  
  3.         StringBuilder bc = new StringBuilder();  
  4.         System.out.println(bc.capacity());  
  5.         bc.append("William Shakespeare");  
  6.         System.out.println(bc.capacity());  
  7.         bc.append("William Shakespeare is a famous writer");  
  8.         System.out.println(bc.capacity());  
  9.         bc.ensureCapacity(20);  
  10.         System.out.println(bc.capacity());  
  11.         bc.ensureCapacity(90);  
  12.         System.out.println(bc.capacity());  
  13.     }  
  14. }  
13

Output

14

Summary

Thus, we learned that StringBuilder class is same as StringBuffer class except that it is non-synchronized in Java and also learn its important methods with example.