Sintassi:
#include <string> string& append( const string& str ); string& append( const char* str ); string& append( const string& str, size_type index, size_type len ); string& append( const char* str, size_type num ); string& append( size_type num, char ch ); string& append( input_iterator start, input_iterator end );
La funzione append() puo' essere usata per eseguire i seguenti task:
str alla fine della stringa corrente.str iniziando dal carattere all'indice specificato dal parametro index e lunga len caratteri alla fine della stringa corrente.num caratteri della stringa str alla fine della stringa corrente. num ripetizioni del carattere ch alla fine della stringa corrente.start ed end alla fine della stringa corrente.
Ad esempio, il seguente frammento di codice appende 10 copie del carattere ! ad una stringa:
string str = "Hello World"; str.append( 10, '!' ); cout << str << endl;
L'output del codice qui sopra e' il seguente:
Hello World!!!!!!!!!!
Nel seguente esempio, append() e' usata per concatenare una sottostringa di una stringa in un altra stringa:
string str1 = "Eventually I stopped caring... "; string str2 = "but that was the '80s so nobody noticed."; str1.append( str2, 25, 15 ); cout << "str1 is " << str1 << endl;
Una volta eseguito, il codice sopra visualizzera' il seguente output:
str1 is Eventually I stopped caring... nobody noticed.