stringbuffermethods.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 38 行

JAVA
38
字号
import java.io.*;
public class StringBufferMethods
{
  public static void main (String[] args) 
  {
    
    StringBuffer str = new StringBuffer("This cannot be");
    int i, numChars;

    System.out.println("The original string is: " + str);
  
    numChars = str.length();
    System.out.println("  This string has " + numChars + " characters.");

      // insert characters
    str.insert(4, " I know");
    System.out.println("The string, after insertion, is now: " + str);
    numChars = str.length();
    System.out.println("  This string has " + numChars + " characters.");

      // replace characters
    str.replace(12, 18, "to");  
    System.out.println("The string, after replacement, is: " + str);
    numChars = str.length();
    System.out.println("  This string has " + numChars + " characters.");
      
      // append characters
    str.append(" correct");
    System.out.println("The string, after appending, is: " + str);
    numChars = str.length();
    System.out.println("  This string has " + numChars + " characters.");
  
      // reverse the characters
    str.reverse();
    System.out.println("The string, after being reversed is: " + str);
  
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?