StringBuilder is a mutable sequence of characters provided by the java.lang package. Unlike String, its contents can be modified without creating a new object for every operation. It is commonly used when a string needs to be changed frequently, especially in single-threaded applications
It provides similar functionality to StringBuffer, but without thread safety.
StringBuilder is not synchronized, so it performs better in single-threaded applications.
Use StringBuffer only when thread safety is required; otherwise, prefer StringBuilder for improved performance.
Java
publicclassGeeks{publicstaticvoidmain(String[]args){StringBuildersb=newStringBuilder("GeeksforGeeks");System.out.println("Initial StringBuilder: "+sb);sb.append(" is awesome!");System.out.println("After append: "+sb);}}
Output
Initial StringBuilder: GeeksforGeeks
After append: GeeksforGeeks is awesome!
Explanation : Above example demonstrates the use of the append() method in StringBuilder to add text at the end of an existing string.
The append() method adds the given string to the end of the existing sequence without creating a new object. This makes StringBuilder efficient for concatenation operations.
Declaration
StringBuilder sb = new StringBuilder("Initial String");
Hierarchy of StringBuilder
StringBuilder extends AbstractStringBuilder and implements Serializable, CharSequence, and Comparable<StringBuilder>.
StringBuilder Class
StringBuilder Constructors
StringBuilder class provides multiple constructors for different use cases.
StringBuilder() : Creates an empty builder with a default capacity of 16 characters.
StringBuilder(int capacity) : Creates an empty builder with a specified initial capacity.
StringBuilder(String str) : Initializes the builder with the content of the given String.
StringBuilder(CharSequence cs) : Initializes the builder with the given CharSequence (for example, String or StringBuffer).
Java
publicclassStringBuilderConstructorsDemo{publicstaticvoidmain(String[]args){StringBuildersb1=newStringBuilder();sb1.append("Hello");System.out.println("sb1: "+sb1);StringBuildersb2=newStringBuilder(50);sb2.append("This has initial capacity 50");System.out.println("sb2: "+sb2);StringBuildersb3=newStringBuilder("Geeks");sb3.append("ForGeeks");System.out.println("sb3: "+sb3);CharSequencecs="Java";StringBuildersb4=newStringBuilder(cs);sb4.append("Programming");System.out.println("sb4: "+sb4);}}
Output
sb1: Hello
sb2: This has initial capacity 50
sb3: GeeksForGeeks
sb4: JavaProgramming
Explanation : This program demonstrates different constructors of the StringBuilder class and how to append data using the append() method.
Commonly Used Methods in StringBuilder
The StringBuilder class provides various methods for string manipulation.
Example: Using StringBuilder Methods
Java
publicclassGeeks{publicstaticvoidmain(String[]args){StringBuildersb=newStringBuilder("GeeksforGeeks");System.out.println("Initial: "+sb);sb.append(" is awesome!");System.out.println("After append: "+sb);sb.insert(13," Java");System.out.println("After insert: "+sb);sb.replace(0,5,"Welcome to");System.out.println("After replace: "+sb);sb.delete(8,14);System.out.println("After delete: "+sb);sb.reverse();System.out.println("After reverse: "+sb);System.out.println("Capacity: "+sb.capacity());System.out.println("Length: "+sb.length());charc=sb.charAt(5);System.out.println("Character at index 5: "+c);sb.setCharAt(5,'X');System.out.println("After setCharAt: "+sb);Stringsub=sb.substring(5,10);System.out.println("Substring (5–10): "+sub);sb.reverse();// Revert for searchSystem.out.println("Index of 'Geeks': "+sb.indexOf("Geeks"));sb.deleteCharAt(5);System.out.println("After deleteCharAt: "+sb);Stringresult=sb.toString();System.out.println("Final String: "+result);}}
Output:
Explanation : This program demonstrates various StringBuilder methods such as append(), insert(), replace(), delete(), reverse(), substring(), and setCharAt() to perform dynamic string manipulation efficiently in Java.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.