Java 中的 append 方法是什么?


java.lang.StringBuffer 的 append(char c) 方法会将 char 参数的字符串表示形式追加到该序列中。该参数会追加到该序列的内容中。该序列的长度会增加 1。

示例

 在线演示

import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      StringBuffer buff = new StringBuffer("tuts ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('A');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
      buff = new StringBuffer("abcd ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('!');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
   }
}

输出

buffer = tuts
After append = tuts A
buffer = abcd
After append = abcd !

更新于: 2019-07-30

3K+ 浏览

开启你的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.