How to work with the StringBuilder class

The associated package:

  • java.util.StringBuilder

Common constructors of the StringBuilder classes:

Constructors Description
StringBuilder() Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(intLength) Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument
StringBuilder(String) Constructs a string builder initialized to the contents of the specified string.

Common methods of the StringBuilder classes:

Method Description
capacity() Returns the current capacity.
length() Returns the length (character count).
setLength(intNumOfChars) Sets the length of the character sequence.
appendValue(value) Adds the specified value to the end of the string.
insert(index, value) Inserts the value into this character sequence.
replace(stringIndex, endIndex, String) Replaces the characters in a substring of this sequence with characters in the specified String.
delete(startIndex, endIndex) Removes the characters in a substring of this sequence.
deleteCharAt(index) Removes the char at the specified position in this sequence.
setCharAt(index, character) The character at the specified index is set to ch.
charAt(index) Returns the char value in this sequence at the specified index.
substring(index) Returns a new String that contains a subsequence of characters currently contained in this character sequence.
substring(startIndex, endIndex) Returns a new String that contains a subsequence of characters currently contained in this sequence.
toString() Returns a string representing the data in this sequence.

Descriptions:

  • StringBuilder objects are mutable, which means you can modify the characters in the string. The capacity of a StringBuilder object is automatically increased if necessary.
  • The append and insert methods accept primitive types, objects and arrays of characters.
  • The StringBuilder class was designed to replace the StringBuffer.

Note:

  • StringBuilder is much faster than String.

Example 1: Code thata creates a phone number

StringBuilder phoneNumber = new StringBuilder;
phoneNumber.append("800");
phoneNumber.append("555");
phoneNumber.append("1212");

Example 2: Code that adds dashes to a phone number

phoneNumber.insert(3, "-");
phoneNumber.insert(7, "-");

Example 3: Code that removes dashes from a phone number

for (int i = 0; i < phoneNumber.length(); i++)
{
if (phoneNumber.charAt(i) == '-')
phoneNumber.deleteCharAt(i–);
}

Example 4:that parses a phone number

StringBuilder phoneNumber = new StringBuilder("800-555-1212");
String areaCode = phoneNumber.substring(0,3);
String prefix = phoneNumber.substring(4,3);
String suffix = phoneNumber.substring(8);

Example 5: Code that shows how caacity automatically increases

StringBuilder name = new StringBuilder(8);
int capacity1 = name.capacity(); // capacity1 = 8
name.append(" My name goes here.");
int length = name.length(); // length = 17
int capacity2 = name.capacity(); // capacity2 is 18 (2 * capacity1 + 2)

More help can be found here: http://www.j2ee.me/javase/6/docs/api/java/lang/StringBuilder.html