Метод StringBuilder append () в Java с примерами

Опубликовано: 13 Февраля, 2022

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-#