logo

StringBuilder metóda append() v jazyku Java s príkladmi

The java.lang.StringBuilder.append() metóda sa používa na pridanie reťazcovej reprezentácie nejakého argumentu do sekvencie. Existuje 13 spôsobov/foriem, v ktorých môže byť metóda append() použitá odovzdávaním rôznych typov argumentov:

    Príloha StringBuilder( booleovský a ): The java.lang.StringBuilder.append( booleovský a ) je vstavaná metóda v jazyku Java, ktorá sa používa na pridanie reťazcovej reprezentácie booleovského argumentu k danej sekvencii.



    Syntax:

    public StringBuilder append( boolean a )>

    Parameter: Táto metóda akceptuje jeden parameter a typu boolean a odkazuje na boolovskú hodnotu, ktorá sa má pripojiť.

    Návratová hodnota: Metóda vráti odkaz na tento objekt.



    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

     Input: Welcome to Geeksforgeeks Output: Welcome to Geeksforgeeks true Input: We fail- Output: We fail- false>
    java.lang.StringBuilder.append( char a ): Toto je metóda vstavaná v jazyku Java, ktorá sa používa na pridanie reťazcovej reprezentácie argumentu char k danej sekvencii. Argument char je pripojený k obsahu tejto sekvencie StringBuilder.

    Syntax:

    public StringBuilder append( char a )>

    Parameter: Metóda akceptuje jeden parameter a čo je hodnota Char, ktorej reťazcová reprezentácia sa má pripojiť.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.
    Príklady:

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

    Nižšie uvedené programy ilustrujú metódu 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);> >}> }>

    >

    zoradený zoznam polí java
    >

    Výkon:

     Welcome geeks! Result after appending = Welcome geeks!T hello world- Result after appending = hello world-#>
    Príloha StringBuilder( char[] astr ): The java.lang.StringBuilder.append( char[] astr ) je vstavaná metóda, ktorá k tejto sekvencii StringBuildera pripája reťazcovú reprezentáciu argumentu poľa znakov.

    Syntax:

    public StringBuilder append( char[] astr )>

    Parameter: Metóda akceptuje jeden parameter astr čo sú sekvencie Char, ktorých reťazcová reprezentácia sa má pripojiť.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.
    Príklady:

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

    Nižšie uvedený program ilustruje java.lang.StringBuilder.append( char[] astr ) metóda:




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

    >

    >

    linuxové príkazy vytvárajú priečinok
    Výkon:

     We are geeks Result after appending = We are geeks GEEkS We are - Result after appending = We are -abcd>
    Príloha StringBuilder( char[] cstr, int iset, int ilength ) : Táto metóda pripojí k tejto sekvencii reťazcovú reprezentáciu podpola argumentu poľa char. Znaky poľa znakov cstr, začínajúce od indexu iset, sa pripájajú v poradí k obsahu tejto sekvencie. Dĺžka tejto sekvencie sa zvyšuje o hodnotu ilength.

    Syntax:

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

    Parameter: Táto metóda akceptuje tri parametre:

    • cstr – Toto sa vzťahuje na sekvenciu Char.
    • Iset – Toto sa týka indexu prvého znaku, ktorý sa má pripojiť.
    • idĺžka – Toto sa vzťahuje na počet znakov, ktoré sa majú pridať.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.
    Nižšie uvedený program ilustruje metódu 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ýkon:

     String Builder before = Geeks After appending String Builder = techcodeview.com>
    Príloha StringBuilder( dvojité a ): Táto metóda jednoducho pripojí reprezentáciu reťazca dvojitého argumentu k tejto sekvencii StringBuilder.

    Syntax:

    public StringBuilder append( double val )>

    Parameter: Metóda akceptuje jeden parameter val ktorý odkazuje na desatinnú hodnotu, ktorej reťazcová reprezentácia sa má pripojiť.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.
    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

     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>
    Príloha StringBuilder( plavák val ): Táto metóda pripojí reťazcovú reprezentáciu argumentu float k tejto sekvencii.

    Syntax:

    public StringBuilder append( float val )>

    Parameter: Metóda akceptuje jeden parameter val čo je plávajúca hodnota, ktorej reťazcová reprezentácia sa má pripojiť.

    Návratová hodnota: StringBuilder.append( plavák val ) metóda vráti referenciu na objekt typu string po vykonaní operácie.

    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

    pole reťazcov v programovaní v c
     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>
    Príloha StringBuilder( int val ) Táto metóda jednoducho pripojí reprezentáciu reťazca argumentu int k tejto sekvencii StringBuilder.
    Syntax:

    public StringBuilder append( int val )>

    Parameter: Metóda akceptuje jeden parameter val čo je celočíselná hodnota, na ktorej sa má operácia vykonať.

    Návratová hodnota: Metóda vráti odkaz na tento objekt.

    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

     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>
    Príloha StringBuilder( Dlhý val ) : Táto metóda jednoducho pripojí reprezentáciu reťazca dlhého argumentu k tejto sekvencii StringBuilder.

    Syntax:

    public StringBuilder append( Long val )>

    Parameter: Metóda akceptuje jeden parameter val čo je dlhá hodnota.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.
    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

     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>
    Príloha StringBuilder( CharSequence a ): Táto metóda sa používa na pripojenie zadanej CharSequence k tejto sekvencii.

    Syntax:

    public StringBuilder append( CharSequence a )>

    Parameter: Metóda akceptuje jeden parameter a čo je hodnota CharSequence.

    Návratová hodnota: Metóda vráti objekt typu reťazec po vykonaní operácie pripojenia.

    Príklady:

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

    Nižšie uvedený program ilustruje metódu java.lang.StringBuilder.append().

    java číslo na reťazec




    // 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ýkon:

     String Builder = Geeksfor After append = Geeksforgeeks>
    Príloha StringBuilder( CharSequence chseq, int začiatok, int koniec ) : Táto metóda sa používa na pripojenie podsekvencie zadanej CharSequence k tomuto StringBuilderu.

    Syntax:

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

    Parameter: Metóda akceptuje tri parametre:

    • chseq (CharSequence): Toto sa vzťahuje na hodnotu CharSequence.
    • začať (Celé číslo): Vzťahuje sa na počiatočný index podsekvencie, ktorá sa má pridať.
    • koniec (Celé číslo): Toto sa vzťahuje na koncový index podsekvencie, ktorá sa má pripojiť.

    Návratová hodnota: Metóda vráti reťazec po vykonaní operácie pripojenia.

    Príklady:

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

    Nižšie uvedený program ilustruje metódu 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ýkon:

     String builder= We are the After append string buffer = We are the geeks>
    Príloha StringBuilder( Objekt obj ) : Táto metóda sa používa na pripojenie reprezentácie reťazca argumentu Object k objektu StringBuilder.

    Syntax:

    StringBuilder append( Object obj )>

    Parameter: Metóda akceptuje jeden parameter obj ktorý odkazuje na objekt, ktorý je potrebné pripojiť.

    Návratová hodnota: Metóda vráti reťazec po vykonaní operácie pripojenia.

    Nižšie uvedený program ilustruje metódu 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);> >}> }>

    návrhové vzory v jazyku Java
    >

    >

    Výkon:

     String Builder = Geeksfor After appending result is = Geeksforgeeks>
    Príloha StringBuilder( Struna istr ) : Táto metóda sa používa na pripojenie zadaného reťazca k tomuto StringBuilderu.
    Syntax:

    StringBuilder append( String istr )>

    Parameter: Metóda akceptuje jeden parameter istr typu String, ktoré odkazujú na hodnotu, ktorá sa má pripojiť.

    Návratová hodnota: Metóda vráti zadaný reťazec tejto sekvencii znakov.
    Nižšie uvedený program ilustruje metódu 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ýkon:

     String Builder = Geeksfor After appending result is = Geeksforgeeks>
    Príloha StringBuilder( StringBuilder sbf ) : Táto metóda sa používa na pripojenie zadaného StringBuildera k tejto sekvencii alebo StringBuilderu.

    Syntax:

    public StringBuilder append( StringBuilder sbf )>

    Parameter: Metóda akceptuje jeden parameter sbf odkazuje na StringBuilder na pridanie.

    Návratová hodnota: Metóda vráti StringBuilder do tejto sekvencie.
    Nižšie uvedený program ilustruje metódu 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ýkon:

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