The Wayback Machine - https://web.archive.org/web/20240726050455/https://www.geeksforgeeks.org/stringbuffer-append-method-in-java-with-examples/
Open In App

StringBuffer append() Method in Java with Examples

Last Updated : 02 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Pre-requisite: StringBuffer class in Java
The java.lang.StringBuffer.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:

  1. StringBuffer append(boolean a) :The java.lang.StringBuffer.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 StringBuffer 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 = "I love my Country" 
    boolean a = true
    
    Output: I love my Country true
    

    Below program illustrates the java.lang.StringBuffer.append() method:




    // Java program 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);
        }
    }

    
    

    Output:

    Input: We are geeks and its really 
    Output: We are geeks and its really true
    
    Input: We are lost - 
    Output: We are lost - false
    
  2. java.lang.StringBuffer.append(char a) : This is an inbuilt method that appends the string representation of the char argument to the given sequence. The char argument is appended to the contents of this StringBuffer sequence.

    Syntax :

    public StringBuffer 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 :
    StringBuffer = I love my Country 
    char a = A
    
    Output: I love my Country A

    Below programs illustrate the java.lang.StringBuffer.append(char a) method.




    // Java program 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);
        }
    }

    
    

    Output:

    We are geeks and its really 
    Result after appending = We are geeks and itsM
    We are lost -
    Result after appending = We are lost -&
    
  3. StringBuffer append(char[] astr): The java.lang.StringBuffer.append(char[] astr) is the inbuilt method which appends the string representation of the char array argument to this StringBuffer sequence.

    Syntax :

    public StringBuffer 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.StringBuffer.append(char[] astr) method:




    // Java program 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);
        }
    }

    
    

    Output:

    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
    
  4. StringBuffer 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 StringBuffer 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.StringBuffer.append(char[] cstr, int iset, int ilength) method.




    // Java program to illustrate the
    // java.lang.StringBuffer append(char[] cstr, int iset, int length)
    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);
        }
    }

    
    

    Output:

    String buffer  before = Geeks
    After appending string buffer = GeeksforGeeks
    
  5. StringBuffer append(double a) : This method simply appends the string representation of the double argument to this StringBuffer sequence.

    Syntax :

    public StringBuffer append(double a)

    Parameter: The method accepts a single parameter a 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 = I love my Country
    Double a = 54.82
    Output: I love my Country 54.82

    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    Output:

    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
    
  6. StringBuffer append(float f) : This method appends the string representation of the float argument to this sequence.

    Syntax :

    public StringBuffer append(float a)

    Parameter: The method accepts a single parameter a which is the float value whose string representation is to be appended.

    Return Value: StringBuffer.append(float a) method returns a reference the string object after the operation is performed.

    Examples :

    Input : 
         StringBuffer  = I love my Country   
    float a =  5.2
     
    Output: I love my Country 5.2

    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    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
    
  7. StringBuffer append(int i :) This method simply appends the string representation of the int argument to this StringBuffer sequence.
    Syntax :

    public StringBuffer append(int a)

    Parameter: The method accepts a single parameter a which is the int value.

    Return Value: The method returns a reference to this object.

    Examples :

    Input :
    StringBuffer = I love my Country  
    int a = 55
    
    Output: I love my Country 55

    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    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
    
  8. StringBuffer append(Long l) : This method simply appends the string representation of the long argument to this StringBuffer sequence.

    Syntax :

    public StringBuffer append(Long a)

    Parameter : The method accepts a single parameter a which is the long value.

    Return Value: The method returns a string object after the append operation is performed.
    Examples :

    Input :
    StringBuffer  = I love my Country
    Long a = 591995
    
    Output: I love my Country 591995

    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    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
    
  9. StringBuffer append(CharSequence a) : This method is used to append the specified CharSequence to this sequence.

    Syntax :

    public StringBuffer 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 :
    StringBuffer  = I love my Country   
    CharSequence a =  abcd
    
    Output : I love my Countryabcd
    

    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    Output:

    string buffer = Geeksfor
    After append = Geeksforgeeks
    
  10. StringBuffer append(CharSequence chseq, int start, int end) : This method is used to append a subsequence of the specified CharSequence to this StringBuffer.

    Syntax :

    StringBuffer append(CharSequence chseq, int start, int end)

    Parameter : The method accepts a three parameter:

    • 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 :
    StringBuffer = Geeksforgeeks
    CharSequence chseq = abcd1234
    int start = 2
    int end = 7
    
    Output :Geeksforgeekscd123
    

    Below program illustrates the java.lang.StringBuffer.append() method:




    // Java program 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);
        }
    }

    
    

    Output:

    string buffer = we are the 
    After append string buffer = we are the geeks
    
  11. StringBuffer append(Object obj) : This method is used to append the string representation of the Object argument to the StringBuffer.

    Syntax :

    StringBuffer 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 programs illustrate the java.lang.StringBuffer.append() method.

    Program :




    // Java program 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);
        }
    }

    
    

    Output:

    string buffer = Geeksfor
    After appending result is = Geeksforgeeks
    
  12. StringBuffer append(String istr) : This method is used to append the specified string to this StringBuffer.
    Syntax :

    StringBuffer 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.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    Output:

    string buffer = Geeksfor
    After appending result is = Geeksforgeeks
    
  13. StringBuffer append(StringBuffer sbf) : This method is used to append the specified StringBuffer to this sequence or StringBuffer.

    Syntax :

    public StringBuffer append(StringBuffer sbf)

    Parameter : The method accepts a single parameter sbf refers to the StringBuffer to append.

    Return Value : The method returns StringBuffer to this sequence.
    Below program illustrates the java.lang.StringBuffer.append() method.




    // Java program 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);
        }
    }

    
    

    Output:

    String buffer 1 = Geeks
    String buffer 2 = forgeeks 
    After appending the result is = Geeksforgeeks
    


Similar Reads

StringBuffer deleteCharAt() Method in Java with Examples
The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of integer type which refers to the index of the char
2 min read
StringBuffer appendCodePoint() Method in Java with Examples
appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an integer value been assigned. --&gt; appendCodePoint(
3 min read
StringBuffer delete() Method in Java with Examples
The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_point, int end_point) Parameters : The method acce
3 min read
StringBuffer replace() Method in Java with Examples
The StringBuffer.replace() is the inbuilt method which is used to replace the characters in a substring of this sequence with the characters in the specified String. Here simply the characters in the substring are removed and other char is inserted at the start.Syntax : public StringBuffer replace(int first, int last, String st) Parameters : The me
2 min read
StringBuffer reverse() Method in Java with Examples
The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the StringBuffer after reversing the characters. Examples
2 min read
StringBuffer codePointCount() method in Java with Examples
The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of the text range and endIndex is index after the la
3 min read
StringBuffer codePointBefore() method in Java with Examples
The codePointBefore() method of StringBuffer class is a method used to take an index as a parameter and returns the “Unicode number” of the character present before that index. The value of index must lie between 0 to length-1. If the char value at (index – 1) is in the low-surrogate range, char at (index – 2) is not negative with value is in the h
2 min read
StringBuffer trimToSize() method in Java with Examples
The trimToSize() method of StringBuffer class is the inbuilt method used to trims the capacity used for the character sequence of StringBuffer object. If the buffer used by StringBuffer object is larger than necessary to hold its current sequence of characters, then this method is called to resize the StringBuffer object for converting this object
2 min read
StringBuffer toString() method in Java with Examples
The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Subsequent changes to this sequence contained by Obje
2 min read
StringBuffer codePointAt() method in Java with Examples
The codePointAt() method of StringBuffer class returns a character Unicode point at that index in sequence contained by StringBuffer. This method returns the “Unicodenumber” of the character at that index. Value of index must be lie between 0 to length-1. If the char value present at the given index lies in the high-surrogate range, the following i
2 min read
three90RightbarBannerImg