append

Syntax:

    #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 );

The append function either:

  • (1&2)appends str on to the end of the current string,
  • (3)appends a substring of str starting at index that is len characters long on to the end of the current string,
  • (4)appends first num characters from str on to string
  • (5)appends num repetitions of ch on to the end of the current string,
  • (6)appends the sequence denoted by start and end on to the end of the current string.

For example, the following code uses append to add 10 copies of the '!' character to a string:

     string str = "Hello World";
     str.append( 10, '!' );
     cout << str << endl;

That code displays:

     Hello World!!!!!!!!!!

In the next example, append() is used to concatenate a substring of one string onto another string:

   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;

When run, the above code displays:

   str1 is Eventually I stopped caring... nobody noticed.