StringBuilder length, setLength, capacity and ensureCapacity methods. - Java Language Basics

Java examples for Language Basics:StringBuilder

Description

StringBuilder length, setLength, capacity and ensureCapacity methods.

Demo Code

public class Main 
{
   public static void main(String[] args)
   {/* www. j  a va2  s.c o  m*/
      StringBuilder buffer = new StringBuilder("Hello, how are you?");

      System.out.printf("buffer = %s\nlength = %d\ncapacity = %d\n\n",
         buffer.toString(), buffer.length(), buffer.capacity());

      buffer.ensureCapacity(75);
      System.out.printf("New capacity = %d\n\n", buffer.capacity());

      buffer.setLength(10);
      System.out.printf("New length = %d\nbuffer = %s\n", 
         buffer.length(), buffer.toString());
   } 
}

Result


Related Tutorials