集册 Java实例教程 StringBuilder的长度,setLength,容量和ensureCapacity方法。

StringBuilder的长度,setLength,容量和ensureCapacity方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

588
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
StringBuilder的长度,setLength,容量和ensureCapacity方法。

public class Main 

{

   public static void main(String[] args)
   /*
    from 时   代    Java - nowjava.com 
   */

   {

      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());

   } 

}/*n o w j a v a . c o m 提供*/