StringBuilder append() Method in Java With Examples

The java.lang.StringBuilder.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used by the passing of various types of arguments:
- StringBuilder append(boolean a) :The java.lang.StringBuilder.append(boolean a) is an inbuilt method in Java which is used to append the string representation of the boolean argument to a given sequence.
Syntax :
public StringBuilder append(boolean a)
Parameter: This method accepts a single parameter a of boolean type and refers to the Boolean value to be appended.
Return Value: The method returns a reference to this object.
Examples:
Input: string_buffer = "We are Indians" boolean a = true Output: We are Indians true
Below program illustrates the java.lang.StringBuilder.append() method:
// Java program to illustrate the// StringBuilder append(boolean a)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sb1 =newStringBuilder("Welcome to Geeksforgeeks ");System.out.println("Input: "+ sb1);// Appending the boolean valuesb1.append(true);System.out.println("Output: "+ sb1);System.out.println();StringBuilder sb2 =newStringBuilder("We fail- ");System.out.println("Input: "+ sb2);// Appending the boolean valuesb2.append(false);System.out.println("Output: "+ sb2);}}Output:Input: Welcome to Geeksforgeeks Output: Welcome to Geeksforgeeks true Input: We fail- Output: We fail- false
- java.lang.StringBuilder.append(char a): This is an inbuilt method in Java which is used to append the string representation of the char argument to the given sequence. The char argument is appended to the contents of this StringBuilder sequence.
Syntax :
public StringBuilder append(char a)
Parameter: The method accepts a single parameter a which is the Char value whose string representation is to be appended.
Return Value: The method returns a string object after the append operation is performed.
Examples :Input : StringBuilder = I love my Country char a = A Output: I love my Country A
Below programs illustrate the java.lang.StringBuilder.append(char a) method.
// Java program to illustrate the// java.lang.StringBuilder.append(char a)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("Welcome geeks!");System.out.println( sbf);/* Here it appends the char argument asstring to the StringBuilder */sbf.append('T');System.out.println("Result after"+" appending = "+ sbf);sbf =newStringBuilder("hello world-");System.out.println(sbf);/* Here it appends the char argument asstring to the String Builder */sbf.append('#');System.out.println("Result after appending = "+ sbf);}}Output:Welcome geeks! Result after appending = Welcome geeks!T hello world- Result after appending = hello world-#
- StringBuilder append(char[] astr): The java.lang.StringBuilder.append(char[] astr) is the inbuilt method which appends the string representation of the char array argument to this StringBuilder sequence.
Syntax :
public StringBuilder append(char[] astr)
Parameter: The method accepts a single parameter astr which are the Char sequence whose string representation is to be appended.
Return Value: The method returns a string object after the append operation is performed.
Examples:Input : StringBuffer = I love my Country char[] astr = 'I', 'N', 'D', 'I', 'A' Output: I love my Country INDIA
Below program illustrates the java.lang.StringBuilder.append(char[] astr) method:
// Java program to illustrate the// java.lang.StringBuilder.append(<em>char[] astr</em>)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("We are geeks ");System.out.println(sbf);// Char arraychar[] astr =newchar[]{'G','E','E','k','S'};/* Appends string representation of chararray to this String Builder */sbf.append(astr);System.out.println("Result after"+" appending = "+ sbf);sbf =newStringBuilder("We are -");System.out.println(sbf);// Char arrayastr =newchar[] {'a','b','c','d'};/* Appends string representation of chararray to this StringBuilder */sbf.append(astr);System.out.println("Result after appending = "+ sbf);}}Output: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) : This method appends the string representation of a subarray of the char array argument to this sequence. The Characters of the char array cstr, starting at index iset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of ilength.
Syntax :
public StringBuilder append(char[] cstr, int iset, int ilength)
Parameter : This method accepts three parameters:
- cstr – This refers to the Char sequence.
- iset – This refers to the index of the first char to append.
- ilength – This refers to the number of chars to append.
Return Value: The method returns a string object after the append operation is performed.
Below program illustrates the java.lang.StringBuilder.append(char[] cstr, int iset, int ilength) method.// Java program to illustrate the// append(char[] cstr, int iset, int ilength)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sb =newStringBuilder("Geeks");System.out.println("String Builder "+"before = "+ sb);char[] cstr =newchar[]{'f','o','r','G','e','e','k','s','q','q'};/* appends the string representation of char arrayargument to this String Builder with offset initiallyat index 0 and length as 8 */sb.append(cstr,0,8);// Print the String Builder after appendingSystem.out.println("After appending String Builder = "+ sb);}}Output:String Builder before = Geeks After appending String Builder = Lazyroar
- StringBuilder append(double a): This method simply appends the string representation of the double argument to this StringBuilder sequence.
Syntax :
public StringBuilder append(double val)
Parameter: The method accepts a single parameter val which refers to the decimal value whose string representation is to be appended.
Return Value: The method returns a string object after the append operation is performed.
Examples :Input : StringBuffer = my Country Double a = 54.82 Output: my Country 54.82
Below program illustrates the java.lang.StringBuilder.append(double val) method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){System.out.println("We are geeks and its really ");StringBuilder sbf =newStringBuilder("We are geeks and its ");// Char arrayDouble astr =newDouble(36.47);/* Here it appends string representation of Doubleargument to this StringBuilder*/sbf.append(astr);System.out.println("Result after appending = "+ sbf);System.out.println("We are lost -");sbf =newStringBuilder("We are lost -");astr =newDouble(27.38);/* Here it appends string representation of Doubleargument to this StringBuilder*/sbf.append(astr);System.out.println("Result after appending = "+ sbf);}}Output: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): This method appends the string representation of the float argument to this sequence.
Syntax :
public StringBuilder append(float val)
Parameter: The method accepts a single parameter val which is the float value whose string representation is to be appended.
Return Value: StringBuilder.append(float val) method returns a reference the string object after the operation is performed.
Examples :
Input : StringBuilder = I love my Country float a = 5.2 Output: I love my Country 5.2
Below program illustrates the java.lang.StringBuilder.append() method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){System.out.println("We are geeks and its really ");StringBuilder sbf =newStringBuilder("We are geeks and its ");Float astr =newFloat(6.47);/* Here it appends string representation ofFloat argument to this StringBuilder */sbf.append(astr);System.out.println("Result after appending = "+sbf);System.out.println("We are lost -");sbf =newStringBuilder("We are lost -");astr =newFloat(27.38);// Here it appends string representation of Float// argument to this String Buildersbf.append(astr);System.out.println("Result after appending = "+sbf);}}Output: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) This method simply appends the string representation of the int argument to this StringBuilder sequence.
Syntax :public StringBuilder append(int val)
Parameter: The method accepts a single parameter val which is an integer value on which the operation is supposed to be performed.
Return Value: The method returns a reference to this object.
Examples :
Input : StringBuilder = I love my Country int a = 55 Output: I love my Country 55
Below program illustrates the java.lang.StringBuilder.append() method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){System.out.println("We are geeks and its really ");StringBuilder sbf =newStringBuilder("We are geeks and its ");Integer astr =newInteger(827);/* Here it appends string representation ofInteger argument to this StringBuilder*/sbf.append(astr);System.out.println("Result after appending = "+sbf);System.out.println("We are lost -");sbf =newStringBuilder("We are lost -");astr =newInteger(515);// Here it appends string representation of Integer// argument to this StringBuildersbf.append(astr);System.out.println("Result after appending = "+sbf);}}Output: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) : This method simply appends the string representation of the long argument to this StringBuilder sequence.
Syntax :
public StringBuilder append(Long val)
Parameter: The method accepts a single parameter val which is the long value.
Return Value: The method returns a string object after the append operation is performed.
Examples :Input : StringBuilder = I love my Country Long a = 591995 Output: I love my Country 591995
Below program illustrates the java.lang.StringBuilder.append() method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){System.out.println("We are geeks and its really ");StringBuilder sbf =newStringBuilder("We are geeks and its ");Long astr =newLong(827);/* Here it appends string representation ofLong argument to this StringBuilder*/sbf.append(astr);System.out.println("Result after appending = "+sbf);System.out.println("We are lost -");sbf =newStringBuilder("We are lost -");astr =newLong(515);/* Here it appends string representation of Longargument to this StringBuilder*/sbf.append(astr);System.out.println("Result after appending = "+sbf);}}Output: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): This method is used to append the specified CharSequence to this sequence.
Syntax :
public StringBuilder append(CharSequence a)
Parameter: The method accepts a single parameter a which is the CharSequence value.
Return Value: The method returns a string object after the append operation is performed.
Examples :
Input : StringBuilder = "I love my Country" CharSequence a = " India" Output : I love my Country India
Below program illustrates the java.lang.StringBuilder.append() method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("Geeksfor");System.out.println("String Builder = "+ sbf);CharSequence chSeq ="geeks";// Appends the CharSequencesbf.append(chSeq);// Print the String Builder after appendingSystem.out.println("After append = "+ sbf);}}Output:String Builder = Geeksfor After append = Geeksforgeeks
- StringBuilder append(CharSequence chseq, int start, int end) : This method is used to append a subsequence of the specified CharSequence to this StringBuilder.
Syntax :
StringBuilder append(CharSequence chseq, int start, int end)
Parameter : The method accepts three parameters:
- chseq(CharSequence): This refers to the CharSequence value.
- start(Integer): This refers to the starting index of the subsequence to be appended..
- end(Integer): This refers to the end index of the subsequence to be appended.
Return Value : The method returns the string after the append operation is performed.
Examples :
Input : StringBuilder = Geeksforgeeks CharSequence chseq = abcd1234 int start = 2 int end = 7 Output :Geeksforgeekscd123
Below program illustrates the java.lang.StringBuilder.append() method:
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("We are the ");System.out.println("String builder= "+ sbf);CharSequence chSeq ="wegeekss";/* It appends the CharSequence withstart index 2 and end index 4 */sbf.append(chSeq,2,7);System.out.println("After append string"+" buffer = "+ sbf);}}Output:String builder= We are the After append string buffer = We are the geeks
- StringBuilder append(Object obj) : This method is used to append the string representation of the Object argument to the StringBuilder.
Syntax :
StringBuilder append(Object obj)
Parameter: The method accepts a single parameter obj which refers to the object needed to be appended.
Return Value: The method returns the string after performing the append operation.
Below program illustrates the java.lang.StringBuilder.append() method.
// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("Geeksfor");System.out.println("String Builder = "+ sbf);Object objectvalue ="geeks";// Here it appends the Object valuesbf.append(objectvalue);System.out.println("After appending result is = "+sbf);}}Output:String Builder = Geeksfor After appending result is = Geeksforgeeks
- StringBuilder append(String istr) : This method is used to append the specified string to this StringBuilder.
Syntax :StringBuilder append(String istr)
Parameter: The method accepts a single parameter istr of String type which refer to the value to be appended.
Return Value : The method returns a specified string to this character sequence.
Below program illustrates the java.lang.StringBuilder.append() method.// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf =newStringBuilder("Geeksfor");System.out.println("String Builder = "+ sbf);String strvalue ="geeks";// Here it appends the Object valuesbf.append(strvalue);System.out.println("After appending result is = "+sbf);}}Output:String Builder = Geeksfor After appending result is = Geeksforgeeks
- StringBuilder append(StringBuilder sbf) : This method is used to append the specified StringBuilder to this sequence or StringBuilder.
Syntax:
public StringBuilder append(StringBuilder sbf)
Parameter: The method accepts a single parameter sbf refers to the StringBuilder to append.
Return Value : The method returns StringBuilder to this sequence.
Below program illustrates the java.lang.StringBuilder.append() method.// Java program to illustrate the// java.lang.StringBuilder.append()importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){StringBuilder sbf1 =newStringBuilder("Geeks");System.out.println("String Builder 1 = "+ sbf1);StringBuilder sbf2 =newStringBuilder("forgeeks ");System.out.println("String Builder 2 = "+ sbf2);// Here it appends String Builder2 to String Builder1sbf1.append(sbf2);System.out.println("After appending the result is = "+sbf1);}}Output:String Builder 1 = Geeks String Builder 2 = forgeeks After appending the result is = Geeksforgeeks



