Java: StringBuilder Class & Examples


Java StringBuilder is used to create a mutable String object. It’s introduced in JDK 1.5. It’s similar to StringBuffer class the only difference is non-synchronized and no thread-safe.

See Also: String Vs StringBuffer Vs StringBuilder

Constructors of StringBuilder class

Constructor Description
StringBuilder() creates an empty string Builder with the default initial capacity of 16.
StringBuilder(String str) creates a string Builder with the given string.
StringBuilder(int length) creates an empty string Builder with the given capacity as length.

Methods of StringBuilder class

Method Description 
public StringBuilder append(String s) Append the specified string with current 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) 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) Replace the string from the specified startIndex and endIndex.
public StringBuilder delete(int startIndex, int endIndex) Delete the string from the specified startIndex and endIndex.
public StringBuilder reverse() Reverse the string.
public int capacity() Return the current capacity of String builder.
public void ensureCapacity(int minimumCapacity) Ensure the capacity at least equal to the given minimum then only increase capacity.
public char charAt(int index) Return the character at the specified index position.
public int length() return the total count of the characters in string.
public String substring(int startIndex) is used to return the substring from the specified startIndex.
public String substring(int startIndex, int endIndex) is used to return the substring from the specified startIndex and endIndex.

Example: StringBuilder append() method

Java StringBuilder append() method used to concatenates the given argument with this string.

StringBuilder sb=new StringBuilder("Facing Issues On ");
sb.append("IT");//Original String will change
System.out.println(sb);//new string "Facing Issues On IT"

Example: StringBuilder insert() method

Java StringBuilder insert() method used to insert the given string at the given position.

StringBuilder sb=new StringBuilder("Facing On IT");
sb.insert(6," Issues ");//insert string on 6th position
System.out.println(sb);//new string "Facing Issues On IT"

Example: StringBuilder replace() method

Java StringBuilder replace() method used to replaces the given string from the specified startIndex and endIndex.

StringBuilder sb=new StringBuilder("Facing Saurabh On IT");
sb.replace(7,14,"Issues");//replace saurabh with  issues
System.out.println(sb);//new string "Facing Issues On IT"

Example: StringBuilder delete() method

Java StringBuilder delete() method use to deletes the string from the specified startIndex to endIndex..

StringBuilder sb=new StringBuilder("Facing Saurabh On IT");
sb.delete(7,14);//delete saurabh
System.out.println(sb);//new string "Facing  On IT"

Example: StringBuilder reverse() method

Java StringBuilder reverse() method used to reverses the current string.

StringBuilder sb=new StringBuilder("Facing Saurabh On IT");
sb.reverse();
System.out.println(sb);//new string "TI no seussI gnicaF"

See Also: Java: Ways to reverse String

Example: StringBuilder capacity() method

Java StringBuilder capacity() method use to returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of the character increases from its current capacity, it increases the capacity by (oldcapacity2)+2. For example, Suppose your current capacity is 16, then the next capacity will be (162)+2=34.

StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.append("Facing Issues On IT");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (old_capacity*2)+2
sb.append("Learn from Others Experinces");
System.out.println(sb.capacity()); //now (34*2)+2=70 i.e (old_capacity*2)+2

Example: StringBuilder ensureCapacity() method

Java StringBuilder ensureCapacity() method use to ensures that the given capacity is the minimum to the current capacity. If it is greater than the ensure capacity, it increases the capacity by (oldcapacity2)+2. For example, Suppose your current capacity is 16, then the next capacity will be (162)+2=34.

StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());//default 16
sb.ensureCapacity(40);//Now Capacity will increase when reach to 40
sb.append("Facing Issues On IT");
System.out.println(sb.capacity());//because of ensure capacity capacity will 40 only
sb.append("Learn from Others Experinces");
System.out.println(sb.capacity()); //now (40*2)+2=82 i.e (oldcapacity*2)+2

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s