Using a StringBuilder Object - Java Language Basics

Java examples for Language Basics:StringBuilder

Description

Using a StringBuilder Object

Demo Code

public class Main {
  public static void main(String[] args) {
    // Create an empty StringNuffer 
    StringBuilder sb = new StringBuilder();
    printDetails(sb);//from www  .ja v  a 2  s  . c om

    // Append "blessings"  
    sb.append("blessings");
    printDetails(sb);

    // Insert "Good " in the beginning  
    sb.insert(0, "Good ");
    printDetails(sb);

    // Delete the first o  
    sb.deleteCharAt(1);
    printDetails(sb);

    sb.append(" test test test");
    printDetails(sb);

    // Set the length to 3 
    sb.setLength(3);
    printDetails(sb);

    // Reverse the content  
    sb.reverse();
    printDetails(sb);
  }

  public static void printDetails(StringBuilder sb) {
    System.out.println("Content: \"" + sb + "\"");
    System.out.println("Length: " + sb.length());
    System.out.println("Capacity: " + sb.capacity());

    // Print an empty line to separate results 
    System.out.println();
  }
}

Related Tutorials