String Builder, Buffer & Tokenizer

String Builder –
  • StringBuilder objects are String objects that can be modified. Hence to create a mutable (modifiable) string object, we use the Java StringBuilder class.
  • StringBuilder is not thread-safe or, in other words, not synchronized. One should prefer using StringBuffer if synchronization is mandatory.
  • The class is designed for use as a drop-in replacement for StringBuffer in places where the String Buffer used by a single thread.
  • Class Hierarchy – java.lang.Object -> java.lang-> Class StringBuilder
  • Constructors of StringBuilder Class –
    • StringBuilder ( ) – This constructor constructs a string builder with no characters in it and an initial capacity of 16 characters.
    • StringBuilder ( int capacity ) – This constructor constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
    • StringBuilder(CharSequence seq) – This constructor constructs a string builder that contains the same characters as the specified CharSequence.
    • StringBuilder ( String str ) – This constructor constructs a string builder initialized to the specified string’s contents. Also, the initial capacity of the string builder is 16 plus the length of the string argument.
  • Some methods of StringBuilder class –
    • append () – It concatenates the given argument(string representation) to the end of the invoking StringBuilder object.
    • insert() – It inserts the given argument(string representation) into the invoking StringBuilder object at the given position.
    • replace() – It replaces the string from a specified start index to the end index.
    • reverse() – It reverses the characters within a StringBuilder object.
    • delete() – It deletes the string from the specified beginIndex to the endIndex.
    • capacity() – It returns the current capacity of the StringBuilder object. 
String Buffer –
  • StringBuffer is a peer class of String that creates mutable (modifiable) string, i.e., it represents growable and writable character sequences.
  • One may insert characters and substrings in the middle or append them to the end in StringBuffer. It will automatically grow to make room for such additions and often pre-allocate more characters than we need to allow room for growth.
  • StringBuffer is thread-safe; most of its methods are synchronized. So multiple threads cannot access or use the StringBuffer object at the same time.
  • Constructors of StringBuffer Class –
    • StringBuffer( ) – This constructor reserves room for 16 characters without reallocation.
    • StringBuffer( int size) – This constructor accepts an integer argument that explicitly sets the buffer’s size.
    • StringBuffer(String str) – This constructor accepts a String argument and sets the StringBuffer object’s initial contents. Also, it reserves room for 16 more characters without reallocation.
  • Some methods of StringBuffer class –
    • length() – It returns the StringBuffer object’s length.
    • capacity() – It returns the capacity of the StringBuffer object.
    • append( ) – It adds text at the end of the existing text.
    • insert( ) – It inserts text at the specified index position.
    • reverse( ) – It reverses the characters within a StringBuffer object. It will return the reversed object on which this method was called.
    • ensureCapacity( ) – It increases the capacity of a StringBuffer object and ensures that the capacity is equal to the given minimum.
String Tokenizer –
  • An application can break a string into tokens with the help of StringTokenizer class. It is a simple way to break a string.
  • It does not have the facility to differentiate between numbers, quoted strings, identifiers, etc.
  • A current position is maintained internally by the StringTokenizer object within the string to be tokenized.
  • Constructors of StringTokenizer Class –
    • StringTokenizer(String str) – It accepts a string that needs to be tokenized. The default delimiters used are new line, space, tab, carriage return, and form feed.
    • StringTokenizer(String str, String delim) – It accepts a string and a set of delimiters used to tokenize the given string.
    • StringTokenizer(String str, String delim, boolean flag) -It accepts a string, set of delimiters, and a flag. If the flag is true, delimiter characters are considered to be tokens. If the flag is false, delimiter characters serve to separate tokens.
  • Some methods of StringTokenizer Class –
    • hasMoreTokens() – It checks if there are more tokens available.
    • nextToken() – It returns the next token from the StringTokenizer object.
    • nextToken(String delim) – It returns the next token based on the delimiter.
    • hasMoreElements() – It is same as hasMoreTokens() method.
    • nextElement() – It is same as nextToken() but its return type is Object.
    • countTokens() – It returns the total number of tokens.