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:
- A character string that can be modified is represented by the StringBuilder Java class. In Java, the StringBuilder class replaces the String class since it generates a mutable sequence of characters rather than an immutable one like the String Class does.
- Due to the fact that both classes build changing character sequences as an alternative to the String Class, their functions are very similar. The StringBuilder class, however, differs from the StringBuffer class in terms of synchronization.
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.
- StringBuilder appendCodePoint(int codePoint): This method appends the string representation of the codePoint argument to this sequence.
- int capacity(): This approach gives the available capacity.
- char charAt(int index): The char value in this sequence at the given index is returned by this procedure.
- IntStream chars(): The java.lang package contains the codePoints() function.String type. A stream of code point values from the supplied sequence is returned. Code points are specific character-specific Unicode code points.
- The character (Unicode code point) before the given index is returned by the method int codePointBefore(int index).
- The method IntStream codePoints() extracts a stream of code point values from this sequence.
- StringBuilder delete(int start, int end): The java.lang.StringBuilder class contains the delete(int start, int end) function. The StringBuilder sequence’s substring in question is deleted. The start and end indices are used to specify the substring.
- StringBuilder deleteCharAt(int index): The java.lang.StringBuilder class contains the deleteCharAt(int index) function. It eliminates the character from the StringBuilder sequence at the provided index.
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:
- StringBuilder is mutable, in contrast to the String class, which is immutable (unchangeable)
- Efficiency – It is mainly optimized for concatenating and manipulating the strings.
- Reduce Memory – StringBuilder is mutable, so it reduces the memory.
Disadvantages of StringBuilder Class:
- Limited function –It only provides a few methods like appending, indexing and deleting
- No built-in methods – It does not provide methods for operations like searching, splitting and replacing.
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.