Java StringBuilder class

The StringBuilder class is mutable sequence of characters. This class is the same sa String however the StringBuilder class provides more versatility because it can be modified. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example we have a variable value which have contents "test", if we use a String class to append a suffix, we would be having the following code

String value = "test"; String output = value + "ing";

See how we assign the new String to a new variable. Not unlike if we use StringBuilder we will be having a shorter because of it’s inherent behaviour as mutable.

StringBuilder value = new StringBuilder("test"); value.append("ing");

Java StringBuilder class is used to create mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.

Important Constructors of StringBuilder class
ConstructorDescription
StringBuilder() creates an empty string Builder with the initial capacity of 16.
StringBuilder(String str) creates a string Builder with the specified string.
StringBuilder(int length) creates an empty string Builder with the specified capacity as length.
Important methods of StringBuilder class
MethodDescription
public StringBuilder append(String s) 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) 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) is used to replace the string from specified startIndex and endIndex.
public StringBuilder delete(int startIndex, int endIndex) is used to delete the string from specified startIndex and endIndex.
public StringBuilder reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
public void ensureCapacity(int minimumCapacity) is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index) is used to return the character at the specified position.
public int length() is used to return the length of the string i.e. total number of characters.
public String substring(int beginIndex) is used to return the substring from the specified beginIndex.
public String substring(int beginIndex, int endIndex) is used to return the substring from the specified beginIndex and endIndex.
Java StringBuilder Examples

See the examples of different methods of StringBuilder class.

1) StringBuilder append() method

The StringBuilder append() method concatenates the given argument with this string.

import java.io.*; public class Test { 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 StringBuilder insert() method inserts the given string with this string at the given position.

import java.io.*; public class Test { 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 StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex.

import java.io.*; public class Test { 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() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.

import java.io.*; public class Test { public static void main(String args[]) { StringBuilder sb=new StringBuilder("Hello"); sb.delete(1,3); System.out.println(sb);//prints Hlo } }


Output:
Hlo
5) StringBuilder reverse() method

The reverse() method of StringBuilder class reverses the current string.

import java.io.*; public class Test { public static void main(String args[]) { StringBuilder sb=new StringBuilder("Hello"); sb.reverse(); System.out.println(sb);//prints olleH } }


Output:
olleH
6) StringBuilder capacity() method

The capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.

import java.io.*; public class Test { 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() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.

import java.io.*; public class Test { 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



Instagram