當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringBuilder append()用法及代碼示例


java.lang.StringBuilder.append()方法用於將某些參數的字符串表示形式附加到序列中。通過傳遞各種類型的參數,可以使用append()方法使用13種方式/形式:

  1. StringBuilder append(boolean a):java.lang.StringBuilder.append(boolean a)是Java中的一種內置方法,用於將boolean參數的字符串表示形式附加到給定序列。

    用法:


    public StringBuilder append(boolean a)

    參數:此方法接受布爾類型的單個參數a,表示要附加的布爾值。

    返回值:該方法返回對此對象的引用。

    例子:

    Input:
    string_buffer = "We are Indians" 
    boolean a = true
    
    Output:We are Indians true
    

    以下示例程序旨在說明java.lang.StringBuilder.append()方法:

    // 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); 
        } 
    }
    輸出:
    Input:Welcome to Geeksforgeeks 
    Output:Welcome to Geeksforgeeks true
    
    Input:We fail- 
    Output:We fail- false
    
  2. java.lang.StringBuilder.append(字符):這是Java中的一種內置方法,用於將char參數的字符串表示形式附加到給定序列。 char參數附加到此StringBuilder序列的內容之後。

    用法:

    public StringBuilder append(char a)

    參數:該方法接受單個參數a,該參數是Char值,該值將附加字符串表示形式。

    返回值:執行附加操作後,該方法返回一個字符串對象。
    Examples:

    Input:
    StringBuilder = I love my Country 
    char a = A
    
    Output:I love my Country A

    以下示例程序旨在說明java.lang.StringBuilder.append(char a)方法。

    // 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); 
        } 
    }
    輸出:
    Welcome geeks!
    Result after appending = Welcome geeks!T
    hello world-
    Result after appending = hello world-#
    
  3. StringBuilder append(char [] astr):java.lang.StringBuilder.append(char [] astr)是內置方法,它將char數組參數的字符串表示形式附加到此StringBuilder序列中。

    用法:

    public StringBuilder append(char[] astr)

    參數:該方法接受單個參數astr,該參數是要附加字符串表示形式的Char序列。

    返回值:執行附加操作後,該方法返回一個字符串對象。
    Examples:

    Input:
    StringBuffer  = I love my Country   
    char[] astr = 'I', 'N', 'D', 'I', 'A'
    
    Output:I love my Country INDIA
    

    以下示例程序旨在說明java.lang.StringBuilder.append(char [] astr)方法:

    // 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); 
        } 
    }
    輸出:
    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):此方法將char數組參數的子數組的字符串表示形式附加到此序列。從索引iset開始的char數組cstr的字符按順序附加到此序列的內容中。該序列的長度增加ilength的值。

    用法:

    public StringBuilder append(char[] cstr, int iset, int ilenght)

    參數:此方法接受三個參數:

    • cstr -這是指字符序列。
    • iset-這是指要附加的第一個char的索引。
    • ilenght -這是指要附加的字符數。

    返回值:執行附加操作後,該方法返回一個字符串對象。
    下麵的程序演示了java.lang.StringBuilder.append(char [] cstr,int iset,int ilength)方法。

    // 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); 
        } 
    }
    輸出:
    String Builder before = Geeks
    After appending String Builder = GeeksforGeeks
    
  5. StringBuilder append(double a):此方法隻是將double參數的字符串表示形式附加到此StringBuilder序列中。

    用法:


    public StringBuilder append(double val)

    參數:該方法接受單個參數val,該參數表示要附加字符串表示形式的十進製值。

    返回值:執行附加操作後,該方法返回一個字符串對象。
    Examples:

    Input:
    StringBuffer = my Country
    Double a = 54.82
    
    Output:my Country 54.82

    下麵的程序演示了java.lang.StringBuilder.append(double val)方法。

    // 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); 
        } 
    }
    輸出:
    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):此方法將float參數的字符串表示形式附加到此序列。

    用法:

    public StringBuilder append(float val)

    參數:該方法接受單個參數val,它是要附加字符串表示形式的float值。

    返回值:執行操作後,StringBuilder.append(float val)方法返回對字符串對象的引用。

    例子:

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

    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    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)此方法隻是將int參數的字符串表示形式附加到此StringBuilder序列中。
    用法:
    public StringBuilder append(int val)

    參數:該方法接受單個參數val,該參數是一個整數值,應在該整數值上執行操作。

    返回值:該方法返回對此對象的引用。

    例子:

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

    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    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):此方法隻是將long參數的字符串表示形式附加到此StringBuilder序列中。

    用法:

    public StringBuilder append(Long val)

    參數:該方法接受單個參數val,它是long值。

    返回值:執行附加操作後,該方法返回一個字符串對象。
    Examples:

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

    以下示例程序旨在說明java.lang.StringBuilder.append()方法。


    // 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); 
        } 
    }
    輸出:
    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):此方法用於將指定的CharSequence追加到此序列。

    用法:

    public StringBuilder append(CharSequence a)

    參數:該方法接受單個參數a,即CharSequence值。

    返回值:執行附加操作後,該方法返回一個字符串對象。

    例子:

    Input:
    StringBuilder  = "I love my Country" 
    CharSequence a = " India"
    
    Output:I love my Country India
    

    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    String Builder = Geeksfor
    After append = Geeksforgeeks
    
  10. StringBuilder append(CharSequence chseq, int start, int end):此方法用於將指定CharSequence的子序列附加到此StringBuilder。

    用法:

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

    參數:該方法接受三個參數:

    • chseq(CharSequence):這是CharSequence值。
    • start(整數):這是指要附加的子序列的起始索引。
    • end(整數):這是指要附加的子序列的結束索引。

    返回值:執行附加操作後,該方法返回字符串。


    例子:

    Input:
    StringBuilder = Geeksforgeeks
    CharSequence chseq = abcd1234
    int start = 2
    int end = 7
    
    Output:Geeksforgeekscd123
    

    以下示例程序旨在說明java.lang.StringBuilder.append()方法:

    // 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); 
        } 
    }
    輸出:
    String builder= We are the 
    After append string buffer = We are the geeks
    
  11. StringBuilder append(Object obj):此方法用於將Object參數的字符串表示形式附加到StringBuilder。

    用法:

    StringBuilder append(Object obj)

    參數:該方法接受單個參數obj,它指向需要附加的對象。

    返回值:該方法在執行附加操作後返回字符串。

    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    String Builder = Geeksfor
    After appending result is = Geeksforgeeks
    
  12. StringBuilder append(String istr):此方法用於將指定的字符串附加到此StringBuilder。
    用法:
    StringBuilder append(String istr)

    參數:該方法接受String類型的單個參數istr,該參數表示要附加的值。


    返回值:該方法將指定的字符串返回到此字符序列。
    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    String Builder = Geeksfor
    After appending result is = Geeksforgeeks
    
  13. StringBuilder append(StringBuilder sbf):此方法用於將指定的StringBuilder附加到此序列或StringBuilder。

    用法:

    public StringBuilder append(StringBuilder sbf)

    參數:該方法接受單個參數sbf,該參數表示要附加的StringBuilder。

    返回值:該方法將StringBuilder返回到此序列。
    以下示例程序旨在說明java.lang.StringBuilder.append()方法。

    // 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); 
        } 
    }
    輸出:
    String Builder 1 = Geeks
    String Builder 2 = forgeeks 
    After appending the result is = Geeksforgeeks
    


相關用法


注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 StringBuilder append() Method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。