Site icon TechVidvan

StringBuilder Class in Java with Examples

java stringbuilder class

Mutable (modifiable) Strings can be created using the Java StringBuilder class. With the exception of being non-synchronized, the Java StringBuilder class is identical to the StringBuffer class. It has been accessible since JDK 1.5.

Explanation:

Syntax:

public final class StringBuilder
    extends Object
    implements Serializable, CharSequence

Class Hierarchy:

java.lang.Object
↳ java.lang
↳ Class StringBuilder

Constructors:

StringBuilder() It creates a String Builder with no characters, and its capacity is 16.
StringBuilder(String str) It creates a string builder and with no characters and its capacity specified capacity argument.
StringBuilder(int length) It creates an empty String Builder and the capacity is specified in length.

Methods in Java StringBuilder

StringBuilder append(X x): This method appends the string representation of the X type argument to the sequence.

Diagram:

Example :

class StringBuilderExample{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.append("World");//now original string is changed  
System.out.println(sb);//prints Hello Java  
}  
}

Output:
Hello World

2. StringBuilder insert() method

By using the StringBuilder insert() method, the specified string is placed beside this string in the specified location.

Example:

Public class StringBuilder{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.insert(1,"World");//now original string is changed  
System.out.println(sb);
}  
}

Output :

HWorldello

3. StringBuilder replace() method

The replace() method of the StringBuilder replaces a string with the specified beginIndex and endIndex.

Example:

Public class StringBuilder{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.replace(1,3,"World");  
System.out.println(sb);//prints HJavalo  
}  
}

Output:
HWorldlo

4.StringBuilder delete() method

The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.

Example:

Public class StringBuilder{  
public static void main(String args[]){  
StringBuilder a=new StringBuilder("Content");  
sb.delete(1,3);  
System.out.println(a);  
}  
}

Output:
Cnent

5.StringBuilder reverse() method

The current string is reversed by the StringBuilder class’s reverse() function.

Example:

Public class StringBuilder{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Writing");  
sb.reverse();  
System.out.println(sb);  
}  
}

Output:
gnitirW

Methods of String ClassBuilder:

public StringBuilder append(String s) It’s used to add this string to the one that’s already there. The append() function has numerous overloads, including append(char), append(boolean), append(int), append(float), append(double), etc..
public StringBuilder insert(int offset, String s) The specified string is inserted at the appropriate location using this string. There are other insert() methods, such insert(int, char), insert(int, boolean), and others.
public StringBuilder replace(int startIndex, int endIndex, String str) This method replaces the string from the startIndex and endIndex values.
public StringBuilder delete(int startIndex, int endIndex) It is used to remove the string from the specified startIndex and endIndex.
public StringBuilder reverse() The string is made backwards using it..
public int capacity() It is used to present the capacity at the moment.
public void ensureCapacity(int minimumCapacity) It is used to ensure that the capacity is at least equal to the minimum value given.
public char charAt(int index) The character at the requested place is returned using it.
public int length() This method returns the string’s length or the total number of characters.
public String substring(int beginIndex) Returning the substring from the provided begin is its purpose. Index

Functions:

Uses of StringBuilder Class:

When you wish to alter a string without generating a new object, you can use the StringBuilder class. The StringBuilder class, for instance, can improve speed when string concatenation is done repeatedly in a loop.

Advantages Of StringBuilder Class:

Disadvantages of StringBuilder Class:

Conclusion

We have extensively learned the StringBuilder Class. The function provides more detailed examples and explains them. It is mainly used with various functions in Java.

Exit mobile version