java.lang.StringBuilder.append()方法用于将某些参数的字符串表示形式附加到序列中。通过传递各种类型的参数,可以使用append()方法使用13种方式/形式:
-
StringBuilder append(boolean a):java.lang.StringBuilder.append(boolean a)是Java中的一种内置方法,用于将boolean参数的字符串表示形式附加到给定序列。
用法:
public StringBuilder append(boolean a)
参数:此方法接受布尔类型的单个参数a,表示要附加的布尔值。
返回值:该方法返回对此对象的引用。
例子:
Input: string_buffer = "We are Indians" boolean a = true Output:We are Indians true
以下示例程序旨在说明java.lang.StringBuilder.append()方法:
// Java program to illustrate the // StringBuilder append(boolean a) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("Welcome to Geeksforgeeks "); System.out.println("Input:" + sb1); // Appending the boolean value sb1.append(true); System.out.println("Output:" + sb1); System.out.println(); StringBuilder sb2 = new StringBuilder("We fail- "); System.out.println("Input:" + sb2); // Appending the boolean value sb2.append(false); System.out.println("Output:" + sb2); } }
输出:Input:Welcome to Geeksforgeeks Output:Welcome to Geeksforgeeks true Input:We fail- Output:We fail- false
- java.lang.StringBuilder.append(字符):这是Java中的一种内置方法,用于将char参数的字符串表示形式附加到给定序列。 char参数附加到此StringBuilder序列的内容之后。
用法:
public StringBuilder append(char a)
参数:该方法接受单个参数a,该参数是Char值,该值将附加字符串表示形式。
返回值:执行附加操作后,该方法返回一个字符串对象。
Examples:Input: StringBuilder = I love my Country char a = A Output:I love my Country A
以下示例程序旨在说明java.lang.StringBuilder.append(char a)方法。
// Java program to illustrate the // java.lang.StringBuilder.append(char a) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("Welcome geeks!"); System.out.println( sbf); /* Here it appends the char argument as string to the StringBuilder */ sbf.append('T'); System.out.println("Result after"+ " appending = " + sbf); sbf = new StringBuilder("hello world-"); System.out.println(sbf); /* Here it appends the char argument as string to the String Builder */ sbf.append('#'); System.out.println("Result after appending = " + sbf); } }
输出:
Welcome geeks! Result after appending = Welcome geeks!T hello world- Result after appending = hello world-#
- StringBuilder append(char [] astr):java.lang.StringBuilder.append(char [] astr)是内置方法,它将char数组参数的字符串表示形式附加到此StringBuilder序列中。
用法:
public StringBuilder append(char[] astr)
参数:该方法接受单个参数astr,该参数是要附加字符串表示形式的Char序列。
返回值:执行附加操作后,该方法返回一个字符串对象。
Examples:Input: StringBuffer = I love my Country char[] astr = 'I', 'N', 'D', 'I', 'A' Output:I love my Country INDIA
以下示例程序旨在说明java.lang.StringBuilder.append(char [] astr)方法:
// Java program to illustrate the // java.lang.StringBuilder.append(<em>char[] astr</em>) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("We are geeks "); System.out.println(sbf); // Char array char[] astr = new char[] { 'G', 'E', 'E', 'k', 'S' }; /* Appends string representation of char array to this String Builder */ sbf.append(astr); System.out.println("Result after"+ " appending = " + sbf); sbf = new StringBuilder("We are -"); System.out.println(sbf); // Char array astr = new char[] { 'a', 'b', 'c', 'd' }; /* Appends string representation of char array to this StringBuilder */ sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
输出:We are geeks Result after appending = We are geeks GEEkS We are - Result after appending = We are -abcd
- StringBuilder append(char [] cstr,int iset,int ilength):此方法将char数组参数的子数组的字符串表示形式附加到此序列。从索引iset开始的char数组cstr的字符按顺序附加到此序列的内容中。该序列的长度增加ilength的值。
用法:
public StringBuilder append(char[] cstr, int iset, int ilenght)
参数:此方法接受三个参数:
- cstr -这是指字符序列。
- iset-这是指要附加的第一个char的索引。
- ilenght -这是指要附加的字符数。
返回值:执行附加操作后,该方法返回一个字符串对象。
下面的程序演示了java.lang.StringBuilder.append(char [] cstr,int iset,int ilength)方法。// Java program to illustrate the // append(char[] cstr, int iset, int ilength) import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Geeks"); System.out.println("String Builder "+ "before = " + sb); char[] cstr = new char[] { 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's','q','q' }; /* appends the string representation of char array argument to this String Builder with offset initially at index 0 and length as 8 */ sb.append(cstr, 0, 8); // Print the String Builder after appending System.out.println("After appending String Builder = " + sb); } }
输出:String Builder before = Geeks After appending String Builder = GeeksforGeeks
- StringBuilder append(double a):此方法只是将double参数的字符串表示形式附加到此StringBuilder序列中。
用法:
public StringBuilder append(double val)
参数:该方法接受单个参数val,该参数表示要附加字符串表示形式的十进制值。
返回值:执行附加操作后,该方法返回一个字符串对象。
Examples:Input: StringBuffer = my Country Double a = 54.82 Output:my Country 54.82
下面的程序演示了java.lang.StringBuilder.append(double val)方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuilder sbf = new StringBuilder("We are geeks and its "); // Char array Double astr = new Double(36.47); /* Here it appends string representation of Double argument to this StringBuilder*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); System.out.println("We are lost -"); sbf = new StringBuilder("We are lost -"); astr = new Double(27.38); /* Here it appends string representation of Double argument to this StringBuilder*/ sbf.append(astr); System.out.println("Result after appending = " + sbf); } }
输出:We are geeks and its really Result after appending = We are geeks and its 36.47 We are lost - Result after appending = We are lost -27.38
- StringBuilder append(float val):此方法将float参数的字符串表示形式附加到此序列。
用法:
public StringBuilder append(float val)
参数:该方法接受单个参数val,它是要附加字符串表示形式的float值。
返回值:执行操作后,StringBuilder.append(float val)方法返回对字符串对象的引用。
例子:
Input: StringBuilder = I love my Country float a = 5.2 Output:I love my Country 5.2
以下示例程序旨在说明java.lang.StringBuilder.append()方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuilder sbf = new StringBuilder("We are geeks and its "); Float astr = new Float(6.47); /* Here it appends string representation of Float argument to this StringBuilder */ sbf.append(astr); System.out.println("Result after appending = "+sbf); System.out.println("We are lost -"); sbf = new StringBuilder("We are lost -"); astr = new Float(27.38); // Here it appends string representation of Float // argument to this String Builder 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
- StringBuilder append(int val)此方法只是将int参数的字符串表示形式附加到此StringBuilder序列中。
用法:public StringBuilder append(int val)
参数:该方法接受单个参数val,该参数是一个整数值,应在该整数值上执行操作。
返回值:该方法返回对此对象的引用。
例子:
Input: StringBuilder = I love my Country int a = 55 Output:I love my Country 55
以下示例程序旨在说明java.lang.StringBuilder.append()方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuilder sbf = new StringBuilder("We are geeks and its "); Integer astr = new Integer(827); /* Here it appends string representation of Integer argument to this StringBuilder*/ sbf.append(astr); System.out.println("Result after appending = "+sbf); System.out.println("We are lost -"); sbf = new StringBuilder("We are lost -"); astr = new Integer(515); // Here it appends string representation of Integer // argument to this StringBuilder 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
- StringBuilder append(Long val):此方法只是将long参数的字符串表示形式附加到此StringBuilder序列中。
用法:
public StringBuilder append(Long val)
参数:该方法接受单个参数val,它是long值。
返回值:执行附加操作后,该方法返回一个字符串对象。
Examples:Input: StringBuilder = I love my Country Long a = 591995 Output:I love my Country 591995
以下示例程序旨在说明java.lang.StringBuilder.append()方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { System.out.println("We are geeks and its really "); StringBuilder sbf = new StringBuilder("We are geeks and its "); Long astr = new Long(827); /* Here it appends string representation of Long argument to this StringBuilder*/ sbf.append(astr); System.out.println("Result after appending = "+sbf); System.out.println("We are lost -"); sbf = new StringBuilder("We are lost -"); astr = new Long(515); /* Here it appends string representation of Long argument to this StringBuilder*/ 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
- StringBuilder append(CharSequence a):此方法用于将指定的CharSequence追加到此序列。
用法:
public StringBuilder append(CharSequence a)
参数:该方法接受单个参数a,即CharSequence值。
返回值:执行附加操作后,该方法返回一个字符串对象。
例子:
Input: StringBuilder = "I love my Country" CharSequence a = " India" Output:I love my Country India
以下示例程序旨在说明java.lang.StringBuilder.append()方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("Geeksfor"); System.out.println("String Builder = " + sbf); CharSequence chSeq = "geeks"; // Appends the CharSequence sbf.append(chSeq); // Print the String Builder after appending System.out.println("After append = " + sbf); } }
输出:String Builder = Geeksfor After append = Geeksforgeeks
- StringBuilder append(CharSequence chseq, int start, int end):此方法用于将指定CharSequence的子序列附加到此StringBuilder。
用法:
StringBuilder append(CharSequence chseq, int start, int end)
参数:该方法接受三个参数:
- chseq(CharSequence):这是CharSequence值。
- start(整数):这是指要附加的子序列的起始索引。
- end(整数):这是指要附加的子序列的结束索引。
返回值:执行附加操作后,该方法返回字符串。
例子:
Input: StringBuilder = Geeksforgeeks CharSequence chseq = abcd1234 int start = 2 int end = 7 Output:Geeksforgeekscd123
以下示例程序旨在说明java.lang.StringBuilder.append()方法:
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("We are the "); System.out.println("String builder= " + 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 builder= We are the After append string buffer = We are the geeks
- StringBuilder append(Object obj):此方法用于将Object参数的字符串表示形式附加到StringBuilder。
用法:
StringBuilder append(Object obj)
参数:该方法接受单个参数obj,它指向需要附加的对象。
返回值:该方法在执行附加操作后返回字符串。
以下示例程序旨在说明java.lang.StringBuilder.append()方法。
// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("Geeksfor"); System.out.println("String Builder = " + sbf); Object objectvalue = "geeks"; // Here it appends the Object value sbf.append(objectvalue); System.out.println("After appending result is = "+sbf); } }
输出:String Builder = Geeksfor After appending result is = Geeksforgeeks
- StringBuilder append(String istr):此方法用于将指定的字符串附加到此StringBuilder。
用法:StringBuilder append(String istr)
参数:该方法接受String类型的单个参数istr,该参数表示要附加的值。
返回值:该方法将指定的字符串返回到此字符序列。
以下示例程序旨在说明java.lang.StringBuilder.append()方法。// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf = new StringBuilder("Geeksfor"); System.out.println("String Builder = " + sbf); String strvalue = "geeks"; // Here it appends the Object value sbf.append(strvalue); System.out.println("After appending result is = "+sbf); } }
输出:String Builder = Geeksfor After appending result is = Geeksforgeeks
- StringBuilder append(StringBuilder sbf):此方法用于将指定的StringBuilder附加到此序列或StringBuilder。
用法:
public StringBuilder append(StringBuilder sbf)
参数:该方法接受单个参数sbf,该参数表示要附加的StringBuilder。
返回值:该方法将StringBuilder返回到此序列。
以下示例程序旨在说明java.lang.StringBuilder.append()方法。// Java program to illustrate the // java.lang.StringBuilder.append() import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuilder sbf1 = new StringBuilder("Geeks"); System.out.println("String Builder 1 = " + sbf1); StringBuilder sbf2 = new StringBuilder("forgeeks "); System.out.println("String Builder 2 = " + sbf2); // Here it appends String Builder2 to String Builder1 sbf1.append(sbf2); System.out.println("After appending the result is = "+sbf1); } }
输出:String Builder 1 = Geeks String Builder 2 = forgeeks After appending the result is = Geeksforgeeks
相关用法
- Java StringBuilder lastIndexOf()用法及代码示例
- Java StringBuilder substring()用法及代码示例
- Java StringBuilder appendCodePoint()用法及代码示例
- Java StringBuilder offsetByCodePoints()用法及代码示例
- Java StringBuilder indexOf()用法及代码示例
- Java StringBuilder trimToSize()用法及代码示例
- Java StringBuilder toString()用法及代码示例
- Java Matcher appendTail(StringBuilder)用法及代码示例
- Java Matcher appendReplacement(StringBuilder, String)用法及代码示例
- Java StringBuffer append()用法及代码示例
- Java PrintWriter append(CharSequence)用法及代码示例
- Java PrintWriter append(CharSequence, int, int)用法及代码示例
- Java PrintStream append(char)用法及代码示例
- Java Writer append(char)用法及代码示例
- Java PrintStream append(CharSequence)用法及代码示例
注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 StringBuilder append() Method in Java With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。