Introduction to StringBuilder Class in Java
StringBuilder class is a java class that is an alternative to String Class. As String Class creates a mutable sequence of characters similarly, StringBuilder Class also creates a mutable sequence of characters. StringBuilder class is one of the important classes to work with the Strings. It was added in java 1.5 to improve the performance. Simply we can say StringBuilder Class is used to create a mutable String. It differs in a way to StringBuffer Class, such as it doesn’t guarantee synchronization. A StringBuilder class is not synchronized, so it is not safe in the case of multithreading. StringBuilder class implementation is faster than the StringBuffer Class implementation.
What is the StringBuilder class in java?
StringBuilder Class is imported by java.lang package. StringBuilder class extends Object class to use its properties & Object class uses Serializable & CharSequence. StringBuilder objects are like String objects. StringBuffer class is created to use as a drop-in replacement for StringBuffer Class. Some of the methods of the StringBuilder class return a reference to themself. Multiple operations can be applied as StringBuilder Class instance in a single statement. This way of operations in a single statement is known as method chaining.
Syntax:
public final class StringBuilder extends Object implements Serializable, CharSequence
Constructors of String Builder Class
StringBuilder class provides constructor & its parameters to work with different Strings.
- StringBuilder(): StringBuilder constructor without any parameter creates a string builder that can hold 16 characters initially.
- StringBuilder(int stringLength): StringBuilder constructor with the specified string length creates a string builder with the specified length.
- StringBuilder(String str): StringBuilder constructor with the specified string creates a string builder with the specified string.
Methods of String Builder Class in Java
StringBuilder class provides some of the important methods to work with the strings.
1. append()
The string passed as a parameter to the append method will be concatenated after the string on which the append method is applied.
Code:
class StringBuilderExample{
//main method of class
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is an example of ");
strSB.append("append method.");
System.out.println(strSB);
}
}
Output:
2. insert()
The string passed as a parameter to the insert method will be inserted at the specified index (passed as a first parameter) of the string on which the insert method is applied.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.insert(10, "java ");
System.out.println(strSB);
}
}
Output:
3. replace()
The string passed as a parameter to the replace method will replace all the characters which come in between the characters at the start & last index. This first & last index is passed as the first & second parameter in the replace method.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.replace(0, 9, "java");
System.out.println(strSB);
}
}
Output:
4. delete()
delete() will replace all the characters which come in between the characters at the start & last index. This first & last index is passed as the first & second parameter in the delete() method.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.delete(0, 10);
System.out.println(strSB);
}
}
Output:
5. reverse()
the reverse() method will reverse the string on which the reverse method is applied.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a program");
strSB.reverse();
System.out.println(strSB);
}
}
Output:
6. capacity()
The default capacity of StringBuilder is 16. The capacity of the builder can be increased by (capacity * n) + n.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder();
System.out.println(strSB.capacity());
strSB.append("This is a program");
System.out.println(strSB.capacity());
}
}
Output:
7. length()
length() method will return the length of the specified string.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
System.out.println(strSB.length());
}
}
Output:
8. deleteCharAt()
deleteCharAt() method will delete the character at the specified index passed as a parameter.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
strSB.deleteCharAt(6);
System.out.println(strSB);
}
}
Output:
9. setCharAt(int index, char ch)
setCharAt() method will set the specified char at the specified index passed as the first parameter while the second parameter will be the character.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
strSB.setCharAt(8, 'n');
System.out.println(strSB);
}
}
Output:
10. indexOf()
This method returns the first position’s index in the string for the specified substring passed as a parameter.
Code:
class StringBuilderExample{
public static void main(String args[]){
StringBuilder strSB = new StringBuilder("This is a java program");
System.out.println(strSB.indexOf("java"));
}
}
Output:
StringBuilder class also has some other methods to work with the string which are listed below setLength(), toString(), trimToSize(), substring() etc.
Conclusion
StringBuilder class has some important methods such as speed & capacity, which other classes don’t have. The use of the StringBuilder class makes manipulation of string much easier. It is a useful class to work with the strings in java. If a string is changing frequently & accessible by a single thread, in that case, StringBuilder is better than other classes like String & StringBuffer.
Recommended Articles
This is a guide to StringBuilder Class in Java. Here we discuss the basic concept, top 10 StringBuilder Class methods in Java, and its code implementation. You may also look at the following articles to learn more –