Home > Java > javaTutorial > body text

StringBuilder Class in Java

WBOY
Release: 2024-08-30 16:01:37
Original
803 people have browsed it

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.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Syntax:

public final class StringBuilder extends Object implements Serializable, CharSequence
Copy after login

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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);
}
}
Copy after login

Output: 

StringBuilder Class in Java

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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());
}
}
Copy after login

Output:

StringBuilder Class in Java

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());
}
}
Copy after login

Output:

StringBuilder Class in Java

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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);
}
}
Copy after login

Output:

StringBuilder Class in Java

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"));
}
}
Copy after login

Output:

StringBuilder Class in Java

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.

The above is the detailed content of StringBuilder Class in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!