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:
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. |
#include <string> void assign( size_type num, const char& val ); void assign( input_iterator start, input_iterator end ); string& assign( const string& str ); string& assign( const char* str ); string& assign( const char* str, size_type num ); string& assign( const string& str, size_type index, size_type len ); string& assign( size_type num, const char& ch );
The deafult assign() function gives the current string the values from start to end, or gives it num copies of val.
In addition to the normal (C++ Lists) assign() functionality that all C++ containers have, strings possess an assign() function that also allows them to:
For example, the following code:
string str1, str2 = "War and Peace"; str1.assign( str2, 4, 3 ); cout << str1 << endl;
displays
and
This function will destroy the previous contents of the string.