先決條件:Java中的StringBuffer類
的java.lang.StringBuffer.append()方法用於將某些參數的字符串表示形式附加到序列中。可以使用13種方式/形式使用append()方法:
-
StringBuffer append(布爾值a):java.lang.StringBuffer.append(boolean a)是Java中的一種內置方法,用於將boolean參數的字符串表示形式附加到給定序列。
用法:
public StringBuffer append(boolean a)
參數:此方法接受布爾類型的單個參數a,表示要附加的布爾值。
返回值:該方法返回對此對象的引用。
例子:
Input: string_buffer = "I love my Country" boolean a = true Output: I love my Country true
以下示例程序旨在說明java.lang.StringBuffer.append()方法:
// Java praogram to illustrate the // StringBuffer append(boolean a) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf1 = new StringBuffer("We are geeks and its really "); System.out.println("Input: " + sbf1); // Appending the boolean value sbf1.append(true); System.out.println("Output: " + sbf1); System.out.println(); StringBuffer sbf2 = new StringBuffer("We are lost - "); System.out.println("Input: " + sbf2); // Appending the boolean value sbf2.append(false); System.out.println("Output: " + sbf2); } }
輸出:Input: We are geeks and its really Output: We are geeks and its really true Input: We are lost - Output: We are lost - false
- java.lang.StringBuffer.append(字符):這是一個內置方法,將char參數的字符串表示形式附加到給定序列。 char參數附加到此StringBuffer序列的內容之後。
用法:
public StringBuffer append(char a)
參數:該方法接受單個參數a,該參數是Char值,該值將附加字符串表示形式。
返回值:執行附加操作後,該方法返回一個字符串對象。
例子:Input : StringBuffer = I love my Country char a = A Output: I love my Country A
以下示例程序旨在說明java.lang.StringBuffer.append(char a)方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append(char a) import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its"); /* Here it appends the char argument as string to the string buffer */ sbf.append('M'); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); /* Here it appends the char argument as string to the string buffer */ sbf.append('&'); System.out.println("Result after appending = " + sbf); } }
輸出:
We are geeks and its really Result after appending = We are geeks and itsM We are lost - Result after appending = We are lost -&
- StringBuffer append(char [] astr):java.lang.StringBuffer.append(char [] astr)是一種內置方法,它將char數組參數的字符串表示形式附加到此StringBuffer序列中。
用法:
public StringBuffer append(char[] astr)
參數:該方法接受單個參數astr,該參數是要附加字符串表示形式的Char序列。
返回值:執行附加操作後,該方法返回一個字符串對象。
例子:Input : StringBuffer = I love my Country char[] astr = 'I', 'N', 'D', 'I', 'A' Output: I love my Country INDIA
以下示例程序旨在說明java.lang.StringBuffer.append(char [] astr)方法:
// Java praogram to illustrate the // java.lang.StringBuffer.append(<em>char[] astr</em>) import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its "); // Char array char[] astr = new char[] { 'G', 'E', 'E', 'k', 'S' }; /* Here it appends string representation of char array argument to this string buffer */ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); // Char array astr = new char[] { 'a', 'b', 'c', 'd' }; /* Here it appends string representation of char array argument to argument to this string buffer */ sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
輸出:We are geeks and its really Result after appending = We are geeks and its GEEkS We are lost - Result after appending = We are lost -abcd
- StringBuffer append(char [] cstr,int iset,int ilength):此方法將char數組參數的子數組的字符串表示形式附加到此序列。從索引iset開始的char數組cstr的字符按順序附加到此序列的內容中。該序列的長度增加ilength的值。
用法:
public StringBuffer append(char[] cstr, int iset, int ilenght)
參數:此方法接受三個參數:
- cstr –這是指字符序列。
- iset–這是指要附加的第一個字符的索引。
- ilenght –這是指要追加的字符數。
返回值:執行附加操作後,該方法返回一個字符串對象。
下麵的程序演示了java.lang.StringBuffer.append(char [] cstr,int iset,int ilength)方法。// Java praogram to illustrate the // java.lang.StringBuffer append(char[] cstr, int iset, int ilength) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Geeks"); System.out.println(" String buffer before = " + sb); char[] cstr = new char[] { 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's', 'b', 'e', 'a', 'g', 'e', 'e', 'k' }; /* appends the string representation of char array argument to this string buffer with offset initially at index 0 and length as 8 */ sb.append(cstr, 0, 8); // Print the string buffer after appending System.out.println("After appending string buffer = " + sb); } }
輸出:String buffer before = Geeks After appending string buffer = GeeksforGeeks
- StringBuffer append(雙倍):此方法隻是將double參數的字符串表示形式附加到此StringBuffer序列中。
用法:
public StringBuffer append(double a)
參數:該方法接受單個參數a,該參數表示要附加字符串表示形式的十進製值。
返回值:執行附加操作後,該方法返回一個字符串對象。
例子:Input : StringBuffer = I love my Country Double a = 54.82 Output: I love my Country 54.82
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its "); // char array Double astr = new Double(636.47); /*Here it appends string representation of Double argument to this string buffer*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); astr = new Double(827.38); /*Here it appends string representation of Double argument to this string buffer*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
輸出:We are geeks and its really Result after appending = We are geeks and its 636.47 We are lost - Result after appending = We are lost -827.38
- StringBuffer append(浮點數f):此方法將float參數的字符串表示形式附加到此序列。
用法:
public StringBuffer append(float a)
參數:該方法接受單個參數a,該參數是要附加字符串表示形式的float值。
返回值:執行操作後,StringBuffer.append(float a)方法返回對字符串對象的引用。
例子:
Input : StringBuffer = I love my Country float a = 5.2 Output: I love my Country 5.2
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its "); Float astr = new Float(6.47); /* Here it appends string representation of Float argument to this string buffer */ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); astr = new Float(27.38); // Here it appends string representation of Float // argument to this string buffer sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
輸出:
We are geeks and its really Result after appending = We are geeks and its 6.47 We are lost - Result after appending = We are lost -27.38
- StringBuffer append(詮釋我:)此方法隻是將int參數的字符串表示形式附加到此StringBuffer序列中。
用法:public StringBuffer append(int a)
參數:該方法接受單個參數a,它是int值。
返回值:該方法返回對此對象的引用。
例子:
Input : StringBuffer = I love my Country int a = 55 Output: I love my Country 55
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its "); Integer astr = new Integer(827); /*Here it appends string representation of Integer argument to argument to this string buffer*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); astr = new Integer(515); // Here it appends string representation of Integer // argument to this string buffer sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
輸出:We are geeks and its really Result after appending = We are geeks and its 827 We are lost - Result after appending = We are lost -515
- StringBuffer append(長l):此方法隻是將long參數的字符串表示形式附加到此StringBuffer序列。
用法:
public StringBuffer append(Long a)
參數:該方法接受單個參數a,該參數為long值。
返回值:執行附加操作後,該方法返回一個字符串對象。
例子:Input : StringBuffer = I love my Country Long a = 591995 Output: I love my Country 591995
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuffer sbf = new StringBuffer("We are geeks and its "); Long astr = new Long(827); /* Here it appends string representation of Long argument to this string buffer*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuffer("We are lost -"); astr = new Long(515); /* Here it appends string representation of Long argument to this string buffer*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
輸出:We are geeks and its really Result after appending = We are geeks and its 827 We are lost - Result after appending = We are lost -515
- StringBuffer append(字符序列a):此方法用於將指定的CharSequence追加到此序列。
用法:
public StringBuffer append(CharSequence a)
參數:該方法接受單個參數a,即CharSequence值。
返回值:執行附加操作後,該方法返回一個字符串對象。
例子:
Input : StringBuffer = I love my Country CharSequence a = abcd Output : I love my Countryabcd
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Geeksfor"); System.out.println(" string buffer = " + sbf); CharSequence chSeq = "geeks"; // Appends the CharSequence sbf.append(chSeq); // Print the string buffer after appending System.out.println("After append = " + sbf); } }
輸出:string buffer = Geeksfor After append = Geeksforgeeks
- StringBuffer append(CharSequence chseq,從int開始,從int結束):此方法用於將指定CharSequence的子序列附加到此StringBuffer。
用法:
StringBuffer append(CharSequence chseq, int start, int end)
參數:該方法接受三個參數:
- chseq(CharSequence):這是CharSequence值。
- start(整數):這是指要附加的子序列的起始索引。
- end(整數):這是指要附加的子序列的結束索引。
返回值:執行附加操作後,該方法返回字符串。
例子:
Input : StringBuffer = Geeksforgeeks CharSequence chseq = abcd1234 int start = 2 int end = 7 Output :Geeksforgeekscd123
以下示例程序旨在說明java.lang.StringBuffer.append()方法:
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("we are the "); System.out.println(" string buffer = " + sbf); CharSequence chSeq = "wegeekss"; /* It appends the CharSequence with start index 2 and end index 4 */ sbf.append(chSeq, 2, 7); System.out.println("After append string buffer = " + sbf); } }
輸出:string buffer = we are the After append string buffer = we are the geeks
- StringBuffer append(對象obj):此方法用於將Object參數的字符串表示形式附加到StringBuffer。
用法:
StringBuffer append(Object obj)
參數:該方法接受單個參數obj,它指向需要附加的對象。
返回值:該方法在執行附加操作後返回字符串。
以下示例程序旨在說明java.lang.StringBuffer.append()方法。
程序:
// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Geeksfor"); System.out.println("string buffer = " + sbf); Object objectvalue = "geeks"; // Here it appends the Object value sbf.append(objectvalue); System.out.println("After appending result is = " + sbf); } }
輸出:string buffer = Geeksfor After appending result is = Geeksforgeeks
- StringBuffer append(字符串istr):此方法用於將指定的字符串附加到此StringBuffer。
用法:
StringBuffer append(String istr)
參數:該方法接受String類型的單個參數istr,該參數表示要附加的值。
返回值:該方法將指定的字符串返回到此字符序列。
以下示例程序旨在說明java.lang.StringBuffer.append()方法。// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Geeksfor"); System.out.println("string buffer = " + sbf); String strvalue = "geeks"; // Here it appends the Object value sbf.append(strvalue); System.out.println("After appending result is = " + sbf); } }
輸出:string buffer = Geeksfor After appending result is = Geeksforgeeks
- StringBuffer append(StringBuffer sbf):此方法用於將指定的StringBuffer附加到此序列或StringBuffer。
用法:
public StringBuffer append(StringBuffer sbf)
參數:該方法接受單個參數sbf,該參數表示要附加的StringBuffer。
返回值:該方法將StringBuffer返回此序列。
以下示例程序旨在說明java.lang.StringBuffer.append()方法。// Java praogram to illustrate the // java.lang.StringBuffer.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf1 = new StringBuffer("Geeks"); System.out.println("String buffer 1 = " + sbf1); StringBuffer sbf2 = new StringBuffer("forgeeks "); System.out.println("String buffer 2 = " + sbf2); // Here it appends stringbuffer2 to stringbuffer1 sbf1.append(sbf2); System.out.println("After appending the result is = " + sbf1); } }
輸出:String buffer 1 = Geeks String buffer 2 = forgeeks After appending the result is = Geeksforgeeks
相關用法
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 StringBuffer append() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。