The Java StringBuilder class is utilized for constructing mutable strings. This class is similar to StringBuffer class, with the distinction that it is not synchronized. It has been accessible since JDK 1.5.
Important Constructors of StringBuilder class
| Constructor | Description |
|---|---|
| StringBuilder() | It creates an empty String Builder with the initial capacity of 16. |
| StringBuilder(String str) | It creates a String Builder with the specified string. |
| StringBuilder(int length) | It creates an empty String Builder with the specified capacity as length. |
Important methods of StringBuilder class
| Method | Description |
|---|---|
| public StringBuilder append(String s) | It is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc. |
| public StringBuilder insert(int offset, String s) | It is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc. |
| public StringBuilder replace(int startIndex, int endIndex, String str) | It is used to replace the string from specified startIndex and endIndex. |
| public StringBuilder delete(int startIndex, int endIndex) | It is used to delete the string from specified startIndex and endIndex. |
| public StringBuilder reverse() | It is used to reverse the string. |
| public int capacity() | It is used to return the current capacity. |
| public void ensureCapacity(int minimumCapacity) | It is used to ensure the capacity at least equal to the given minimum. |
| public char charAt(int index) | It is used to return the character at the specified position. |
| public int length() | It is used to return the length of the string i.e. total number of characters. |
| public String substring(int beginIndex) | It is used to return the substring from the specified beginIndex. |
| public String substring(int beginIndex, int endIndex) | It is used to return the substring from the specified beginIndex and endIndex. |
Java StringBuilder Examples
Let's explore some illustrations showcasing various techniques of the StringBuilder class.
1) StringBuilder append method
The StringBuilder append function combines the provided parameter with the current String.
StringBuilderExample.java
class StringBuilderExample{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Output:
Hello Java
2) StringBuilder insert method
The insert method of StringBuilder is used to place a specified string into another string at a specified position.
StringBuilderExample2.java
class StringBuilderExample2{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
Output:
HJavaello
3) StringBuilder replace method
The replace method of StringBuilder is used to substitute a specified string starting from the indicated beginIndex up to the endIndex.
StringBuilderExample3.java
class StringBuilderExample3{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
Output:
HJavalo
4) StringBuilder delete method
The delete function within the StringBuilder class is used to remove a portion of the string starting from the specified beginIndex up to the endIndex.
StringBuilderExample4.java
class StringBuilderExample4{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
Output:
5) StringBuilder reverse method
The reverse function of the StringBuilder class modifies the existing string by reversing its content.
StringBuilderExample5.java
class StringBuilderExample5{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}
}
Output:
6) StringBuilder capacity method
The capacity function in the StringBuilder class provides the current capacity of the Builder object. By default, a Builder object has a capacity of 16 characters. When the number of characters exceeds the current capacity, the capacity is expanded following the formula (old capacity 2) + 2. For instance, if the current capacity is 16, the new capacity will be calculated as (16 2) + 2 = 34.
StringBuilderExample6.java
class StringBuilderExample6{
public static void main(String args[]){
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("Java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
Output:
16
16
34
7) StringBuilder ensureCapacity method
The ensureCapacity function in the StringBuilder class guarantees that the specified capacity becomes the new minimum capacity. In cases where this new capacity exceeds the current capacity, the function expands the capacity by calculating it as (oldCapacity 2) + 2. For instance, if the current capacity is 16, the new capacity will be (16 2) + 2 = 34.
StringBuilderExample7.java
class StringBuilderExample7{
public static void main(String args[]){
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("Java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}
}
Output:
16
16
34
34
70