集册 Java实例教程 使用StringBuilder对象

使用StringBuilder对象

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

499
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用StringBuilder对象

public class Main {

  public static void main(String[] args) {

    // Create an empty StringNuffer 

    StringBuilder sb = new StringBuilder();/*from 时 代 J a v a 公 众 号 - nowjava.com*/

    printDetails(sb);


    // 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);
//nowjava.com - 时  代  Java

    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(
展开阅读全文