StringBuffer append() 方法在 Java 中有何作用?
StringBuffer append() 方法會將特定引數的 String 形式追加到序列中。它是 java.lang.StringBuffer 類的某個方法。該方法會返回一個指向物件自身地址的引用。
append() 方法的基本語法如下 −
public StringBuffer append(data_type variable_name)
下面是一個示範 append() 方法用法的程式 −
示例
import java.lang.*; public class Example { public static void main(String[] args) { StringBuffer s1 = new StringBuffer("The sun rises in the east "); System.out.println("Statement : " + s1); s1.append(true); System.out.println("Outcome : " + s1+"
"); StringBuffer s2 = new StringBuffer("Hello "); System.out.println("Statement : " + s2); s2.append("World"); System.out.println("Output: " + s2+"
"); char c = 'A'; StringBuffer s3 = new StringBuffer("Apple starts with "); System.out.println("Statement : " + s3); s3.append(c); System.out.println("Output : " + s3); } }
輸出
Statement : The sun rises in the east Outcome : The sun rises in the east true Statement : Hello Output: Hello World Statement : Apple starts with Output : Apple starts with A
廣告