String Builder, Buffer & Tokenizer

1. String Builder

  • StringBuilder objects are String objects where we can modify the value of the object. 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() constructor constructs a string builder with no characters in it and an initial capacity of 16 characters.
    • StringBuilder(int capacity) constructor constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
    • StringBuilder(CharSequence seq) constructor constructs a string builder that contains the same characters as the specified CharSequence.
    • StringBuilder(String str) 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 () concatenates the given argument(string representation) to the end of the invoking StringBuilder object.
    • insert() inserts the given argument(string representation) into the invoking StringBuilder object at the given position.
    • replace() replaces the string from a specified start index to the end index.
    • reverse() reverses the characters within a StringBuilder object.
    • delete() deletes the string from the specified beginIndex to the endIndex.
    • capacity() returns the current capacity of the StringBuilder object. 

2. 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() constructor reserves room for 16 characters without reallocation.
    • StringBuffer(int size) constructor accepts an integer argument that explicitly sets the buffer’s size.
    • StringBuffer(String str) 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() returns the StringBuffer object’s length.
    • capacity() returns the capacity of the StringBuffer object.
    • append( ) adds text at the end of the existing text.
    • insert( ) inserts text at the specified index position.
    • reverse( ) reverses the characters within a StringBuffer object. It will return the object with reverse values.
    • ensureCapacity( ) increases the capacity of a StringBuffer object and ensures that the capacity is equal to the given minimum.

3. 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) 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) accepts a string and a set of delimiters used to tokenize the given string.
    • StringTokenizer(String str, String delim, boolean flag) 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() checks if there are more tokens available.
    • nextToken() returns the next token from the StringTokenizer object.
    • nextToken(String delim) returns the next token based on the delimiter.
    • hasMoreElements() is same as hasMoreTokens() method.
    • nextElement() is same as nextToken() but its return type is Object.
    • countTokens() returns the total number of tokens.

Scroll to Top