Metodo Append() di StringBuilder in Java con esempi
IL java.lang.StringBuilder.append() Il metodo viene utilizzato per aggiungere la rappresentazione di stringa di alcuni argomenti alla sequenza. Esistono 13 modi/forme in cui il metodo append() può essere utilizzato passando vari tipi di argomenti:
- StringBuilder append( booleano a ): Java.lang.StringBuilder.append( booleano a ) è un metodo integrato in Java che viene utilizzato per aggiungere la rappresentazione di stringa dell'argomento booleano a una determinata sequenza.
- cstr – Si riferisce alla sequenza Char.
- ho impostato – Si riferisce all'indice del primo carattere da aggiungere.
- ilength – Si riferisce al numero di caratteri da aggiungere.
- chseq (CharSequence): si riferisce al valore CharSequence.
- inizio (Intero): si riferisce all'indice iniziale della sottosequenza da aggiungere..
- FINE (Intero): si riferisce all'indice finale della sottosequenza da aggiungere.
Sintassi:
public StringBuilder append( boolean a )
Parametro: Questo metodo accetta un singolo parametro UN di tipo booleano e fa riferimento al valore booleano da aggiungere.
Valore di ritorno: Il metodo restituisce un riferimento a questo oggetto.
Esempi:
Input: string_buffer = 'We are Indians' boolean a = true Output: We are Indians true
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
Input: Welcome to Geeksforgeeks Output: Welcome to Geeksforgeeks true Input: We fail- Output: We fail- falsejava.lang.StringBuilder.append( carattere a ): Questo è un metodo integrato in Java che viene utilizzato per aggiungere la rappresentazione di stringa dell'argomento char alla sequenza data. L'argomento char viene aggiunto al contenuto di questa sequenza StringBuilder.
Sintassi:
public StringBuilder append( char a )
Parametro: Il metodo accetta un singolo parametro UN che è il valore Char la cui rappresentazione di stringa deve essere accodata.
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuilder = I love my Country char a = A Output: I love my Country A
I programmi seguenti illustrano il metodo 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);> > }> }> |
Produzione:
Welcome geeks! Result after appending = Welcome geeks!T hello world- Result after appending = hello world-#StringBuilder append( char[] astr ): java.lang.StringBuilder.append( char[] astr ) è il metodo integrato che aggiunge la rappresentazione di stringa dell'argomento dell'array di caratteri a questa sequenza StringBuilder.
Sintassi:
public StringBuilder append( char[] astr )
Parametro: Il metodo accetta un singolo parametro astr che sono la sequenza Char la cui rappresentazione di stringa deve essere accodata.
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuffer = I love my Country char[] astr = 'I', 'N', 'D', 'I', 'A' Output: I love my Country INDIA
Il programma seguente illustra java.lang.StringBuilder.append( char[] astr ) metodo:
// 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);> > }> }> |
Produzione:
We are geeks Result after appending = We are geeks GEEkS We are - Result after appending = We are -abcdStringBuilder append( char[] cstr, int iset, int ilength ): questo metodo aggiunge la rappresentazione di stringa di un sottoarray dell'argomento dell'array di caratteri a questa sequenza. I caratteri dell'array di caratteri cstr, a partire dall'indice iset, vengono aggiunti, in ordine, al contenuto di questa sequenza. La lunghezza di questa sequenza aumenta del valore di ilength.
Sintassi:
public StringBuilder append( char[] cstr, int iset, int ilength )
Parametro: Questo metodo accetta tre parametri:
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String Builder before = Geeks After appending String Builder = techcodeview.comStringBuilder append( doppio a ): questo metodo aggiunge semplicemente la rappresentazione di stringa del doppio argomento a questa sequenza StringBuilder.
Sintassi:
public StringBuilder append( double val )
Parametro: Il metodo accetta un singolo parametro val che si riferisce al valore decimale la cui rappresentazione di stringa deve essere accodata.
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuffer = my Country Double a = 54.82 Output: my Country 54.82
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
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.38StringBuilder append( valore galleggiante ): questo metodo aggiunge la rappresentazione di stringa dell'argomento float a questa sequenza.
Sintassi:
public StringBuilder append( float val )
Parametro: Il metodo accetta un singolo parametro val che è il valore float la cui rappresentazione di stringa deve essere accodata.
Valore di ritorno: StringBuilder.append( valore galleggiante ) restituisce un riferimento all'oggetto stringa dopo l'esecuzione dell'operazione.
Esempi:
Input : StringBuilder = I love my Country float a = 5.2 Output: I love my Country 5.2
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
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.38StringBuilder append( val.int ) Questo metodo aggiunge semplicemente la rappresentazione di stringa dell'argomento int a questa sequenza StringBuilder.
Sintassi:
public StringBuilder append( int val )
Parametro: Il metodo accetta un singolo parametro val che è un valore intero su cui si suppone venga eseguita l'operazione.
Valore di ritorno: Il metodo restituisce un riferimento a questo oggetto.
Esempi:
Input : StringBuilder = I love my Country int a = 55 Output: I love my Country 55
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
We are geeks and its really Result after appending = We are geeks and its 827 We are lost - Result after appending = We are lost -515StringBuilder append( Val lunga ): questo metodo aggiunge semplicemente la rappresentazione di stringa dell'argomento lungo a questa sequenza StringBuilder.
Sintassi:
public StringBuilder append( Long val )
Parametro: Il metodo accetta un singolo parametro val che è il valore lungo.
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuilder = I love my Country Long a = 591995 Output: I love my Country 591995
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
We are geeks and its really Result after appending = We are geeks and its 827 We are lost - Result after appending = We are lost -515StringBuilder append( CharSequence a ): questo metodo viene utilizzato per aggiungere il CharSequence specificato a questa sequenza.
Sintassi:
public StringBuilder append( CharSequence a )
Parametro: Il metodo accetta un singolo parametro UN che è il valore CharSequence.
Valore di ritorno: Il metodo restituisce un oggetto stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuilder = 'I love my Country' CharSequence a = ' India' Output : I love my Country India
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String Builder = Geeksfor After append = GeeksforgeeksStringBuilder append( CharSequence chseq, int inizio, int fine ): questo metodo viene utilizzato per aggiungere una sottosequenza del CharSequence specificato a questo StringBuilder.
Sintassi:
StringBuilder append( CharSequence chseq, int start, int end )
Parametro: Il metodo accetta tre parametri:
Valore di ritorno : Il metodo restituisce la stringa dopo l'esecuzione dell'operazione di accodamento.
Esempi:
Input : StringBuilder = Geeksforgeeks CharSequence chseq = abcd1234 int start = 2 int end = 7 Output :Geeksforgeekscd123
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String builder= We are the After append string buffer = We are the geeksStringBuilder append( Oggetto ogg ): questo metodo viene utilizzato per aggiungere la rappresentazione di stringa dell'argomento Object a StringBuilder.
Sintassi:
StringBuilder append( Object obj )
Parametro: Il metodo accetta un singolo parametro ogg che si riferisce all'oggetto che deve essere aggiunto.
Valore di ritorno: Il metodo restituisce la stringa dopo aver eseguito l'operazione di aggiunta.
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String Builder = Geeksfor After appending result is = GeeksforgeeksStringBuilder append( Stringa istr ): questo metodo viene utilizzato per aggiungere la stringa specificata a questo StringBuilder.
Sintassi:
StringBuilder append( String istr )
Parametro: Il metodo accetta un singolo parametro istr di tipo String che fanno riferimento al valore da aggiungere.
Valore di ritorno : Il metodo restituisce una stringa specificata in questa sequenza di caratteri.
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String Builder = Geeksfor After appending result is = GeeksforgeeksStringBuilder append( StringBuilder sbf ): questo metodo viene utilizzato per aggiungere lo StringBuilder specificato a questa sequenza o StringBuilder.
Sintassi:
public StringBuilder append( StringBuilder sbf )
Parametro: Il metodo accetta un singolo parametro sbf si riferisce allo StringBuilder da aggiungere.
Valore di ritorno : Il metodo restituisce StringBuilder a questa sequenza.
Il programma seguente illustra il metodo 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);> > }> }> |
Produzione:
String Builder 1 = Geeks String Builder 2 = forgeeks After appending the result is = Geeksforgeeks