Search

17.6 — std::string appending

Appending

Appending strings to the end of an existing string is easy using either operator+=, append(), or push_back() function.

string& string::operator+= (const string& str)
string& string::append (const string& str)

  • Both functions append the characters of str to the string.
  • Both function return *this so they can be “chained”.
  • Both functions throw a length_error exception if the result exceeds the maximum number of characters.

Sample code:

Output:

one two three

There’s also a flavor of append() that can append a substring:

string& string::append (const string& str, size_type index, size_type num)

  • This function appends num characters from str, starting at index, to the string.
  • Returns *this so it can be “chained”.
  • Throws an out_of_range if index is out of bounds
  • Throws a length_error exception if the result exceeds the maximum number of characters.

Sample code:

Output:

one three

Operator+= and append() also have versions that work on C-style strings:

string& string::operator+= (const char* str)
string& string::append (const char* str)

  • Both functions append the characters of str to the string.
  • Both function return *this so they can be “chained”.
  • Both functions throw a length_error exception if the result exceeds the maximum number of characters.
  • str should not be NULL.

Sample code:

Output:

one two three

There is an additional flavor of append() that works on C-style strings:

string& string::append (const char* str, size_type len)

  • Appends the first len characters of str to the string.
  • Returns *this so they can be “chained”.
  • Throw a length_error exception if the result exceeds the maximum number of characters.
  • Ignores special characters (including ”)

Sample code:

Output:

one three

This function is dangerous and its use is not recommended.

There is also a set of functions that append characters. Note that the name of the non-operator function to append a character is push_back(), not append()!

string& string::operator+= (char c)
void string::push_back (char c)

  • Both functions append the character c to the string.
  • Operator += returns *this so it can be “chained”.
  • Both functions throw a length_error exception if the result exceeds the maximum number of characters.

Sample code:

Output:

one 2

Now you might be wondering why the name of the function is push_back() and not append(). This follows a naming convention used for stacks, where push_back() is the function that adds a single item to the end of the stack. If you envision a string as a stack of characters, using push_back() to add a single character to the end makes sense. However, the lack of an append() function is inconsistent in my view!

It turns out there is an append() function for characters, that looks like this:

string& string::append (size_type num, char c)

  • Adds num occurrences of the character c to the string
  • Returns *this so it can be “chained”.
  • Throws a length_error exception if the result exceeds the maximum number of characters.

Sample code:

Output:

aaabbbb

There’s one final flavor of append() that you won’t understand unless you know what iterators are. If you’re not familiar with iterators, you can ignore this function.

string& string::append (InputIterator start, InputIterator end)

  • Appends all characters from the range [start, end) (including start up to but not including end)
  • Returns *this so it can be “chained”.
  • Throws a length_error exception if the result exceeds the maximum number of characters.

17.7 -- std::string inserting
Index
17.5 -- std::string assignment and swapping

7 comments to 17.6 — std::string appending

  • Always great to see new updates to the site, it has always proven to be an invaluable resource for me.

    Just one issue I’ve spotted here, the example for string::push_back(char c) is attempting to use string::append instead.

  • How to split a string into elements of the array or the list by spesial symbol (like |). As in php the operator “split” or delphi “commtext”?

    Thanks, good tutorial

  • octavsly

    In one of the examples above (also shown below for easier identification) there is a missing space after one.Instead of “one” should be “one “. Otherwise the output is not correct.

    Sample code:

    1 string sString("one");
    2
    3 sString.append("threefour", 5);
    4 cout << sString << endl;

    Output:

    one three

  • Spotted a small mistake: When explaining push_back (and that there isn’t an append with chars), you use this in the example code:

  • Bogdan

    Should be

  • Yep! As more people pointed out (Florian and Bogdan), in the push_back section you used

    when it should be

    Loving the tutorials though šŸ˜›

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter