Java StringBuilder
StringBuilder in Java is a mutable sequence of characters. Unlike the String class, which is immutable, StringBuilder objects can be modified after they are created. This makes StringBuilder more efficient for scenarios where multiple string modifications are required.
Key Topics
1. Creating a StringBuilder
A StringBuilder can be created using the default constructor or by passing an initial string.
Example
public class StringBuilderExample {
public static void main(String[] args) {
// Using default constructor
StringBuilder sb1 = new StringBuilder();
// Using constructor with initial string
StringBuilder sb2 = new StringBuilder("Hello");
System.out.println("sb1: " + sb1);
System.out.println("sb2: " + sb2);
}
}
Output:
sb2: Hello
2. append()
The append()
method adds text to the end of the current StringBuilder object.
Example
public class StringBuilderAppendExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append(", ");
sb.append("World!");
System.out.println(sb.toString());
}
}
Output:
3. insert()
The insert()
method inserts text at the specified index in the current StringBuilder object.
Example
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World!");
sb.insert(5, ",");
System.out.println(sb.toString());
}
}
Output:
4. replace()
The replace()
method replaces the characters in a substring of the current StringBuilder object with the specified string.
Example
public class StringBuilderReplaceExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
sb.replace(7, 12, "Java");
System.out.println(sb.toString());
}
}
Output:
5. delete()
The delete()
method removes the characters in a substring of the current StringBuilder object.
Example
public class StringBuilderDeleteExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
sb.delete(5, 7);
System.out.println(sb.toString());
}
}
Output:
6. reverse()
The reverse()
method reverses the sequence of characters in the current StringBuilder object.
Example
public class StringBuilderReverseExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
sb.reverse();
System.out.println(sb.toString());
}
}
Output:
7. toString()
The toString()
method converts the current StringBuilder object to a String object.
Example
public class StringBuilderToStringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
String str = sb.toString();
System.out.println(str);
}
}
Output:
Key Takeaways
- StringBuilder is used for creating and manipulating mutable strings, providing better performance for multiple modifications.
- Common methods include append(), insert(), replace(), delete(), reverse(), and toString().
- StringBuilder is preferred over String when frequent modifications are required.