StringBuilder

In this chapter you will learn:

  1. What is the difference between StringBuilder and StringBuffer
  2. How to StringBuilder's Constructors to create StringBuilder
  3. How to create StringBuilder with initial String value

StringBuilder vs StringBuffer

StringBuilder is identical to StringBuffer except it is not synchronized. StringBuilder is not thread-safe. StringBuilder is faster than StringBuffer.

StringBuilder creation

We can create StringBuilder with the following four constructors.

  • StringBuilder()
    Creates a string builder and an initial capacity of 16 characters.
  • StringBuilder(CharSequence seq)
    Creates a string builder from the specified CharSequence.
  • StringBuilder(int capacity)
    Creates a string builder with no characters in it and an initial capacity specified by the capacity argument.
  • StringBuilder(String str)
    Creates a string builder initialized to the contents of the specified string.

The following code creates a StringBuilder with initial capacity set to 20.

public class Main {
  public static void main(String[] argv) {
    StringBuilder sb = new StringBuilder(20);
    sb.append("java2s.com");
    //from j  av a  2s  .  c  o  m
    System.out.println(sb.capacity());

  }
}

The output:

Create StringBuilder with initial String value

Another way to create StringBuilder is to use String value.

public class Main {
  public static void main(String[] args) {
    StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit amet.");
    System.out.println("lipsum = " + lipsum.toString());
//from   j a  va2  s  .  co m
    lipsum.delete(0, 8);
    System.out.println("lipsum = " + lipsum.toString());

    lipsum.deleteCharAt(lipsum.length() - 1);
    System.out.println("lipsum = " + lipsum.toString());
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to append data to the end of a StringBuilder
  2. How to chain append methods together
  3. How to insert data to the middle of a StringBuilder
  4. Combine append and insert methods
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words