Java中的StringBuffer append()方法及其示例

先决条件: Java中的StringBuffer类 这个 JAVAlang.StringBuffer。附加() 方法用于将某个参数的字符串表示形式附加到序列中。append()方法有13种使用方式/形式:

null
  1. 字符串缓冲区附加( 布尔a ) : 爪哇。lang.StringBuffer。附加( 布尔a )是Java中的一个内置方法,用于将布尔参数的字符串表示形式附加到给定序列。

    语法:

    public StringBuffer append(boolean a)

    参数: 这个方法只接受一个参数 A. 属于布尔类型,指要追加的布尔值。

    返回值: 该方法返回对此对象的引用。

    例如:

    Input: 
    string_buffer = "I love my Country" 
    boolean a = true
    
    Output: I love my Country true
    

    下面的程序演示了java。lang.StringBuffer。append()方法:

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

    
    

    输出:

    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. JAVAlang.StringBuffer。附加( 查拉 ) : 这是一个内置方法,将char参数的字符串表示形式附加到给定序列。char参数被追加到此StringBuffer序列的内容。

    语法:

    public StringBuffer append(char a)

    参数: 该方法只接受一个参数 A. 这是要附加其字符串表示形式的Char值。

    返回值: 执行追加操作后,该方法返回一个字符串对象。 例如:

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

    下面的程序演示了java。lang.StringBuffer。append(char)方法。

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

    
    

    输出:

    We are geeks and its really 
    Result after appending = We are geeks and itsM
    We are lost -
    Result after appending = We are lost -&
    

  3. 字符串缓冲区附加( char[]astr ): 爪哇。lang.StringBuffer。附加( char[]astr )是将char数组参数的字符串表示形式附加到此StringBuffer序列的内置方法。

    语法:

    public StringBuffer append(char[] astr)

    参数: 该方法只接受一个参数 应科院 这是要追加其字符串表示形式的字符序列。

    返回值: 执行追加操作后,该方法返回一个字符串对象。 例如:

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

    下面的程序演示了java。lang.StringBuffer。附加( char[]astr )方法:

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

    
    

    输出:

    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. 字符串缓冲区附加( 字符[]cstr,整数iset,整数长度 ) : 此方法将char数组参数的子数组的字符串表示形式附加到此序列。从索引iset开始的字符数组cstr的字符按顺序追加到此序列的内容。这个序列的长度增加了ilength的值。

    语法:

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

    参数: 此方法接受三个参数:

    • cstr –这是指字符序列。
    • 伊塞特 –这是指要附加的第一个字符的索引。
    • 伊伦赫特 –这是指要附加的字符数。

    返回值: 执行追加操作后,该方法返回一个字符串对象。 下面的程序演示了java。lang.StringBuffer。append(char[]cstr,int-iset,int-ilength)方法。

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

    
    

    输出:

    String buffer  before = Geeks
    After appending string buffer = GeeksforGeeks
    

  5. 字符串缓冲区附加( 双a ) : 此方法只是将双参数的字符串表示形式附加到此StringBuffer序列。

    语法:

    public StringBuffer append(double a)

    参数: 该方法只接受一个参数 A. 它指的是要附加其字符串表示形式的十进制值。

    返回值: 执行追加操作后,该方法返回一个字符串对象。 例如:

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

    下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    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. 字符串缓冲区附加( 浮动f ) : 此方法将浮点参数的字符串表示形式附加到此序列。

    语法:

    public StringBuffer append(float a)

    参数: 该方法只接受一个参数 A. 它是要附加其字符串表示形式的浮点值。

    返回值: StringBuffer。附加( 漂浮 )方法在执行操作后返回对字符串对象的引用。

    例如:

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

    下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    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. 字符串缓冲区附加( int i: ) 此方法只是将int参数的字符串表示形式附加到此StringBuffer序列。 语法:
    public StringBuffer append(int a)

    参数: 该方法只接受一个参数 A. 这是int值。

    返回值: 该方法返回对此对象的引用。

    例如:

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

    下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    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. 字符串缓冲区附加( 长l ) : 此方法只是将长参数的字符串表示形式附加到此StringBuffer序列。

    语法:

    public StringBuffer append(Long a)

    参数: 该方法只接受一个参数 A. 这就是长期价值。

    返回值: 执行追加操作后,该方法返回一个字符串对象。 例如:

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

    下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    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. 字符串缓冲区附加( CharSequence a ) : 此方法用于将指定的CharSequence追加到此序列。

    语法:

    public StringBuffer append(CharSequence a)

    参数: 该方法只接受一个参数 A. 这是CharSequence值。

    返回值: 执行追加操作后,该方法返回一个字符串对象。

    例如:

    Input :
    StringBuffer  = I love my Country   
    CharSequence a =  abcd
    
    Output : I love my Countryabcd
    

    下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    string buffer = Geeksfor
    After append = Geeksforgeeks
    

  10. 字符串缓冲区附加( 字符序列chseq,整数开始,整数结束 ) : 此方法用于将指定CharSequence的子序列附加到此StringBuffer。

    语法:

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

    参数: 该方法接受三个参数:

    • chseq (CharSequence):这是指CharSequence值。
    • 开始 (整数):指要追加的子序列的起始索引。。
    • 终止 (整数):指要追加的子序列的结束索引。

    返回值: 该方法在执行追加操作后返回字符串。

    例如:

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

    下面的程序演示了java。lang.StringBuffer。append()方法:

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

    
    

    输出:

    string buffer = we are the 
    After append string buffer = we are the geeks
    

  11. 字符串缓冲区附加( 对象对象对象 ) : 此方法用于将对象参数的字符串表示形式附加到StringBuffer。

    语法:

    StringBuffer append(Object obj)

    参数: 该方法只接受一个参数 obj 它指的是需要附加的对象。

    返回值: 该方法在执行追加操作后返回字符串。

    下面的程序演示了java。lang.StringBuffer。append()方法。

    节目:

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

    
    

    输出:

    string buffer = Geeksfor
    After appending result is = Geeksforgeeks
    

  12. 字符串缓冲区附加( 字符串istr ) : 此方法用于将指定的字符串附加到此StringBuffer。 语法:
    StringBuffer append(String istr)

    参数: 该方法只接受一个参数 istr 指要追加的值的字符串类型。

    返回值: 该方法将指定的字符串返回到此字符序列。 下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

    string buffer = Geeksfor
    After appending result is = Geeksforgeeks
    

  13. 字符串缓冲区附加( StringBuffer sbf ) : 此方法用于将指定的StringBuffer附加到此序列或StringBuffer。

    语法:

    public StringBuffer append(StringBuffer sbf)

    参数: 该方法只接受一个参数 sbf 指要追加的StringBuffer。

    返回值: 该方法将StringBuffer返回到此序列。 下面的程序演示了java。lang.StringBuffer。append()方法。

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

    
    

    输出:

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

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享