Friday, December 27, 2024
Google search engine
HomeLanguagesJavaStringBuilder append() Method in Java With Examples

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:

  1. 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)
    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);
        }
    }

    
    
    Output:

    Input: Welcome to Geeksforgeeks 
    Output: Welcome to Geeksforgeeks true
    
    Input: We fail- 
    Output: We fail- false
    
  2. 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)
    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);
        }
    }

    
    
    Output:

    Welcome geeks!
    Result after appending = Welcome geeks!T
    hello world-
    Result after appending = hello world-#
    
  3. 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>)
    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);
        }
    }

    
    
    Output:

    We are geeks  
    Result after appending = We are geeks  GEEkS
    We are  -
    Result after appending = We are  -abcd
    
  4. 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)
    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);
        }
    }

    
    
    Output:

    String Builder before = Geeks
    After appending String Builder = Lazyroar
    
  5. 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()
    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);
        }
    }

    
    
    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
    
  6. 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()
    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);
        }
    }

    
    
    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. 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()
    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);
        }
    }

    
    
    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. 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()
    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);
        }
    }

    
    
    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. 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()
    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);
        }
    }

    
    
    Output:

    String Builder = Geeksfor
    After append = Geeksforgeeks
    
  10. 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()
      
    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);
        }
    }

    
    
    Output:

    String builder= We are the 
    After append string buffer = We are the geeks
    
  11. 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()
      
    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);
        }
    }

    
    
    Output:

    String Builder = Geeksfor
    After appending result is = Geeksforgeeks
    
  12. 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()
      
    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);
        }
    }

    
    
    Output:

    String Builder = Geeksfor
    After appending result is = Geeksforgeeks
    
  13. 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()
      
    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);
        }
    }

    
    
    Output:

    String Builder 1 = Geeks
    String Builder 2 = forgeeks 
    After appending the result is = Geeksforgeeks
    
Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments