Friday, 24 December 2021

Java String Builder class

It is a jdk 1.5 specification that means this class is added to java in the release of jdk 1.5, like StringBuffer class, StringBuilder class objects are also mutable.

The main differences between these two things are 

1.StringBuffer class is synchronized and StringBuilder is not.

2.In case of mutithreading StringBuffer is more reliable than StringBuilder.

3. In case of single thread execution StringBuilder is preferable, as it improves execution of time.

Here are the StringBuilder constructors 

  1. StringBuilder():- Constructs a string builder with no characters in it and an initial capacity of 16 characters.
  2. StringBuilder(CharSequence seq):- Constructs a string builder that contains the same characters as the specified CharSequence.
  3. StringBuilder(int capacity):- Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
  4. StringBuilder(String str):- Constructs a string builder initialized to the contents of the specified string.

There are so may useful methods are available in java StringBuilder, here I am presenting some of the most frequently used methods. If you want complete method and their description you can visit the official website.

  1. append(char c) - Appends the string representation of the char argument to this sequence.
  2. capacity() - Returns the current capacity.
  3. charAt(int index) - Returns the char value in this sequence at the specified index.
  4. deleteCharAt(int index) - Removes the char at the specified position in this sequence.
  5. indexOf(String str, int fromIndex) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
  6. length() - Returns the length (character count).
  7. reverse() - Causes this character sequence to be replaced by the reverse of the sequence.
  8. setCharAt(int index, char ch) - The character at the specified index is set to ch.
  9. substring(int start) - Returns a new String that contains a subsequence of characters currently contained in this character sequence.
  10. toString() - Returns a string representing the data in this sequence.


Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial


C Programming


Java Tutorial


Data Structures


MS Office


Database Management