StringBuilder metoda append() v Javě s příklady

The java.lang.StringBuilder.append() metoda se používá k připojení řetězcové reprezentace nějakého argumentu k sekvenci. Existuje 13 způsobů/forem, kterými lze metodu append() použít předáváním různých typů argumentů:

    StringBuilder append( booleovské a ): The java.lang.StringBuilder.append( booleovské a ) je vestavěná metoda v Javě, která se používá k připojení řetězcové reprezentace booleovského argumentu k dané sekvenci.

    Syntaxe:

    public StringBuilder append( boolean a ) 

    Parametr: Tato metoda přijímá jeden parametr A typu boolean a odkazuje na booleovskou hodnotu, která má být připojena.

    Návratová hodnota: Metoda vrací odkaz na tento objekt.

    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     Input: Welcome to Geeksforgeeks Output: Welcome to Geeksforgeeks true Input: We fail- Output: We fail- false 
    java.lang.StringBuilder.append( char a ): Toto je vestavěná metoda v Javě, která se používá k připojení řetězcové reprezentace argumentu char k dané sekvenci. Argument char je připojen k obsahu této sekvence StringBuilder.

    Syntaxe:

    public StringBuilder append( char a ) 

    Parametr: Metoda přijímá jeden parametr A což je hodnota Char, jejíž reprezentace řetězce má být připojena.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.
    Příklady:

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

    Níže uvedené programy ilustrují metodu 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);> > }> }>

    Výstup:

     Welcome geeks! Result after appending = Welcome geeks!T hello world- Result after appending = hello world-# 
    StringBuilder append( char[] astr ): The java.lang.StringBuilder.append( char[] astr ) je vestavěná metoda, která k této sekvenci StringBuilderu připojuje reprezentaci řetězce argumentu pole char.

    Syntaxe:

    public StringBuilder append( char[] astr ) 

    Parametr: Metoda přijímá jeden parametr astr což jsou sekvence Char, k nimž má být připojena reprezentace řetězce.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.
    Příklady:

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

    Níže uvedený program ilustruje java.lang.StringBuilder.append( char[] astr ) metoda:




    // Java program to illustrate the> // java.lang.StringBuilder.append( char[] astr )> 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);> > }> }>

    Výstup:

     We are geeks Result after appending = We are geeks GEEkS We are - Result after appending = We are -abcd 
    StringBuilder append( char[] cstr, int iset, int ilength ) : Tato metoda připojí k této sekvenci řetězcovou reprezentaci podpole argumentu pole char. Znaky cstr pole char, počínaje indexem iset, jsou připojeny v pořadí k obsahu této sekvence. Délka této sekvence se zvětší o hodnotu ilength.

    Syntaxe:

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

    Parametr : Tato metoda přijímá tři parametry:

    • cstr – To se týká sekvence Char.
    • Iset – Toto se týká indexu prvního znaku, který se má připojit.
    • idélka – Toto se týká počtu znaků, které mají být připojeny.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.
    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     String Builder before = Geeks After appending String Builder = techcodeview.com 
    StringBuilder append( dvojité a ): Tato metoda jednoduše připojí reprezentaci řetězce dvojitého argumentu k této sekvenci StringBuilderu.

    Syntaxe:

    public StringBuilder append( double val ) 

    Parametr: Metoda přijímá jeden parametr val což odkazuje na desetinnou hodnotu, jejíž reprezentace řetězce má být připojena.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.
    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     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 
    StringBuilder append( plovák val ): Tato metoda připojí k této sekvenci řetězcovou reprezentaci argumentu float.

    Syntaxe:

    public StringBuilder append( float val ) 

    Parametr: Metoda přijímá jeden parametr val což je plovoucí hodnota, jejíž řetězcová reprezentace má být připojena.

    Návratová hodnota: StringBuilder.append( plovák val ) metoda vrací odkaz na objekt typu string po provedení operace.

    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     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 
    StringBuilder append( int val ) Tato metoda jednoduše připojí reprezentaci řetězce argumentu int k této sekvenci StringBuilderu.
    Syntaxe:

    public StringBuilder append( int val ) 

    Parametr: Metoda přijímá jeden parametr val což je celočíselná hodnota, na které má být operace provedena.

    Návratová hodnota: Metoda vrací odkaz na tento objekt.

    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     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 
    StringBuilder append( Dlouhý val ) : Tato metoda jednoduše připojí reprezentaci řetězce dlouhého argumentu k této sekvenci StringBuilderu.

    Syntaxe:

    public StringBuilder append( Long val ) 

    Parametr: Metoda přijímá jeden parametr val což je dlouhá hodnota.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.
    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     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 
    StringBuilder append( CharSequence a ): Tato metoda se používá k připojení zadané CharSequence k této sekvenci.

    Syntaxe:

    public StringBuilder append( CharSequence a ) 

    Parametr: Metoda přijímá jeden parametr A což je hodnota CharSequence.

    Návratová hodnota: Metoda vrací objekt typu řetězec po provedení operace připojení.

    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     String Builder = Geeksfor After append = Geeksforgeeks 
    StringBuilder append( CharSequence chseq, int začátek, int konec ) : Tato metoda se používá k připojení podsekvence zadané CharSequence k tomuto StringBuilderu.

    Syntaxe:

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

    Parametr : Metoda přijímá tři parametry:

    • chseq (CharSequence): Toto se týká hodnoty CharSequence.
    • Start (Celé číslo): Toto se týká počátečního indexu podsekvence, která má být připojena.
    • konec (Integer): Toto odkazuje na koncový index podsekvence, která má být připojena.

    Návratová hodnota: Metoda vrací řetězec po provedení operace připojení.

    Příklady:

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

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     String builder= We are the After append string buffer = We are the geeks 
    StringBuilder append( Objekt obj ) : Tato metoda se používá k připojení řetězcové reprezentace argumentu Object k StringBuilderu.

    Syntaxe:

    StringBuilder append( Object obj ) 

    Parametr: Metoda přijímá jeden parametr obj který odkazuje na objekt, který je třeba připojit.

    Návratová hodnota: Metoda vrací řetězec po provedení operace připojení.

    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     String Builder = Geeksfor After appending result is = Geeksforgeeks 
    StringBuilder append( Struna istr ) : Tato metoda se používá k připojení zadaného řetězce k tomuto StringBuilderu.
    Syntaxe:

    StringBuilder append( String istr ) 

    Parametr: Metoda přijímá jeden parametr istr typu String, které odkazují na hodnotu, která má být připojena.

    Návratová hodnota: Metoda vrací zadaný řetězec do této sekvence znaků.
    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

     String Builder = Geeksfor After appending result is = Geeksforgeeks 
    StringBuilder append( StringBuilder sbf ) : Tato metoda se používá k připojení zadaného StringBuilderu k této sekvenci nebo StringBuilderu.

    Syntax:

    public StringBuilder append( StringBuilder sbf ) 

    Parametr: Metoda přijímá jeden parametr sbf odkazuje na StringBuilder k připojení.

    Návratová hodnota: Metoda vrací StringBuilder do této sekvence.
    Níže uvedený program ilustruje metodu 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);> > }> }>

    Výstup:

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