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.
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.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsSyntax:
public final class StringBuilder extends Object implements Serializable, CharSequence
StringBuilder class provides constructor & its parameters to work with different Strings.
StringBuilder class provides some of the important methods to work with the strings.
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:
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:
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:
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:
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:
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:
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:
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:
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:
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.
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.
The above is the detailed content of StringBuilder Class in Java. For more information, please follow other related articles on the PHP Chinese website!