Java StringBuilder Class
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
| 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) | Appends 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) | Inserts 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) | Replaces the string from specified startIndex to endIndex. |
public StringBuilder delete(int startIndex, int endIndex) | Deletes the string from specified startIndex to endIndex. |
public StringBuilder reverse() | Reverses the string. |
public int capacity() | Returns the current capacity. |
public void ensureCapacity(int minimumCapacity) | Ensures the capacity is at least equal to the given minimum. |
public char charAt(int index) | Returns the character at the specified position. |
public int length() | Returns the length of the string i.e. total number of characters. |
public String substring(int beginIndex) | Returns the substring from the specified beginIndex. |
public String substring(int beginIndex, int endIndex) | Returns the substring from the specified beginIndex to endIndex. |
Java StringBuilder Examples
1 StringBuilder append() method
The append() method concatenates the given argument with this String.
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 Java2 StringBuilder insert() method
The insert() method inserts the given string with this string at the given position.
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:
HJavaello3 StringBuilder replace() method
The replace() method replaces the given string from the specified beginIndex to endIndex.
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:
HJavalo4 StringBuilder delete() method
The delete() method deletes the string from the specified beginIndex to endIndex.
class StringBuilderExample4 {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Hello");
sb.delete(1, 3);
System.out.println(sb); // prints Hlo
}
}Output:
Hlo5 StringBuilder reverse() method
The reverse() method reverses the current string.
class StringBuilderExample5 {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Hello");
sb.reverse();
System.out.println(sb); // prints olleH
}
}Output:
olleH6 StringBuilder capacity() method
The capacity() method returns the current capacity of the Builder. The default capacity is 16. If the number of characters increases from its current capacity, it increases the capacity by (oldCapacity * 2) + 2.
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
}
}Output:
16
16
347 StringBuilder ensureCapacity() method
The ensureCapacity() method ensures the given capacity is the minimum current capacity. If greater, it increases capacity by (oldCapacity * 2) + 2.
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
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