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 =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 =newStringBuilder("We fail- ");       ÂSystem.out.println("Input: "+ sb2);       Â// Appending the boolean value       Âsb2.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 =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 =newStringBuilder("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);   Â}}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 =new                     ÂStringBuilder("We are geeks ");       ÂSystem.out.println(sbf);       Â// Char array       Âchar[] astr =newchar[]                         Â{'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 =newStringBuilder("We are -");               ÂSystem.out.println(sbf);       Â// Char array       Âastr =newchar[] {'a','b','c','d'};       Â/* Appends string representation of char                  Âarray 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 =new                             ÂStringBuilder("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 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);   Â}}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 =new             ÂStringBuilder("We are geeks and its ");       Â// Char array       ÂDouble astr =newDouble(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 =newStringBuilder("We are lost -");       Âastr =newDouble(27.38);       Â/* Here it appends string representation of Double Â          Âargument 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 =new                   ÂStringBuilder("We are geeks and its ");       ÂFloat astr =newFloat(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 =newStringBuilder("We are lost -");       Âastr =newFloat(27.38);       Â// Here it appends string representation of Float       Â// argument to this String Builder       Â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 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 =new                   ÂStringBuilder("We are geeks and its ");       ÂInteger astr =newInteger(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 =newStringBuilder("We are lost -");       Âastr =newInteger(515);       Â// Here it appends string representation of Integer       Â// argument 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(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 =newÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂStringBuilder("We are geeks and its ");ÂÂÂ Â Â Â Â Â Â ÂLong astr =newLong(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 =newStringBuilder("We are lost -");ÂÂÂ Â Â Â Â Â Â Âastr =newLong(515);ÂÂÂ Â Â Â Â Â Â Â/* Here it appends string representation of LongÂÂ Â Â Â Â Â Â Â Â Â Âargument 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 CharSequence       Âsbf.append(chSeq);       Â// Print the String Builder after appending       ÂSystem.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 =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);   Â}}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 value       Âsbf.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 value       Âsbf.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 Builder1Â Â Â Â Â Â Â Âsbf1.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
