StringBuilder Class Methods In Java

StringBuilder Class Methods In Java | In Java StringBuilder class was defined in JDK 1.5 version. For storing characters String class and StringBuffer classes were already available since the Java1.0 version but there were some limitations with both of these classes. The string class is immutable and the StringBuffer class is synchronized. To overcome these limitations StringBuilder class was defined in Java 1.5 version.

The StringBuilder class is mutable means we can modify the StringBuilder class object without creating a new object. And all the methods of the StringBuilder class are non-synchronized and therefore provide good performance in single thread applications.

Following are the StringBuilder Class Methods In Java with their purpose or description:-

MethodDescription
int compareTo(StringBuilder another)Compares two StringBuilder instances lexicographically. This method follows the same rules for lexicographical comparison as defined in the CharSequence.compare(this, another)} method.
StringBuilder delete(int start, int end)Removes the characters in a substring of this sequence.
StringBuilder deleteCharAt(int index)Removes the char at the specified position in this sequence. This sequence is shortened by one char.
StringBuilder replace(int start, int end, String str)Replaces the characters in a substring of this sequence with characters in the specified String.
int indexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
int lastIndexOf(String str)Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().
int lastIndexOf(String str, int fromIndex)Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
StringBuilder reverse()It causes this character sequence to be replaced by the reverse of the sequence.
String toString()Returns a string representing the data in this sequence.

The append() method of the StringBuilder class appends the specified string to this character sequence. StringBuilder class has the following overloaded forms of append() method:-

  1. public StringBuilder append(Object obj)
  2. public StringBuilder append(String str)
  3. public StringBuilder append(StringBuffer sb)
  4. public StringBuilder append(CharSequence s)
  5. public StringBuilder append(CharSequence s, int start, int end)
  6. public StringBuilder append(char[ ] str)
  7. public StringBuilder append(char[ ] str, int offset, int len)
  8. public StringBuilder append(boolean b)
  9. public StringBuilder append(char c)
  10. public StringBuilder append(int i)
  11. public StringBuilder append(long lng)
  12. public StringBuilder append(float f)
  13. public StringBuilder append(double d)

Example of the StringBuilder class append() method,

public class Main {
    public static void main(String[] args) {
        StringBuilder string = new StringBuilder();
        string.append("Hello");
        string.append(',');
        string.append(2030);
        string.append(" ");
        string.append(50.99);
        string.append(true);
        System.out.println(string);
    }
}

Output:-

Hello,2030 50.99true

The StringBuilder class insert() method inserts the string representation of the boolean argument into this sequence. StringBuilder class has the following overloaded forms of insert() method:-

  1. public StringBuilder insert(int offset, float flt)
  2. public StringBuilder insert(int offset, int i)
  3. public StringBuilder insert(int offset, long lng)
  4. public StringBuilder insert(int offset, Object objt)
  5. public StringBuilder insert(int offset, String string)
  6. public StringBuilder insert(int ind, char[ ] array, int offset, int len)
  7. public StringBuilder insert(int dstOffset, CharSequence seq)
  8. public StringBuilder insert(int dstOffset, CharSequence seq, int begin, int end)
  9. public StringBuilder insert(int offset, double dbl)
  10. public StringBuilder insert(int offset, boolean bool)
  11. public StringBuilder insert(int offset, char c)
  12. public StringBuilder insert(int offset, char[ ] array)

Example of the StringBuilder class insert() method,

public class Main {
    public static void main(String[] args) {
        StringBuilder string = new StringBuilder("Hello, World!");
        string.insert(0, true);
        System.out.println(string);
        string.insert(1, "Hi");
        System.out.println(string);
    }
}

Output:-

trueHello, World!
tHirueHello, World!

StringBuilder Class Methods In Java Defined in AbstractStringBuilder Class

The StringBuilder Class extends AbstractStringBuilder. The AbstractStringBuilder class also contains many methods for string handling. Therefore they are also available for StringBuilder objects.

MethodDescription
int length()Returns the length or character count.
int capacity()Returns the current capacity.
void ensureCapacity(int minimumCapacity)Ensures that the capacity is at least equal to the specified minimum.
void trimToSize()Attempts to reduce storage used for the character sequence.
void setLength(int newLength)Sets the length of the character sequence.
char charAt(int index)Returns the char value in this sequence at the specified index.
int codePointAt(int index)Returns the character (Unicode code point) at the specified index.
int codePointBefore(int index)Returns the character (Unicode code point) before the specified index.
int codePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range of this sequence.
int offsetByCodePoints(int index, int codePointOffset)Returns the index within this sequence that is offset from the given index by code points.
void getChars(int srcBegin, int srcEnd, char[ ] dst, int dstBegin)Characters are copied from this sequence into the destination character array dst.
void setCharAt(int index, char ch)The character at the specified index is set to ch.
AbstractStringBuilder appendCodePoint(int codePoint)Appends the string representation of the codePoint argument to this sequence.
String substring(int start)Returns a new String that contains a subsequence of characters currently contained in this character sequence.
String substring(int start, int end)Returns a new String that contains a subsequence of characters currently contained in this character sequence.
CharSequence subSequence(int start, int end)Returns a new character sequence that is a subsequence of this sequence.

Other Methods:-

  • IntStream chars()
  • IntStream codePoints()

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *