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)
Sample code:
string sString("one");
sString += string(" two");
string sThree(" three");
sString.append(sThree);
cout << sString << endl;
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)
Sample code:
string sString("one ");
const string sTemp("twothreefour");
sString.append(sTemp, 3, 5); // append substring of sTemp starting at index 3 of length 5
cout << sString << endl;
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)
Sample code:
string sString("one");
sString += " two";
sString.append(" three");
cout << sString << endl;
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)
Sample code:
string sString("one");
sString.append("threefour", 5);
cout << sString << endl;
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)
Sample code:
string sString("one");
sString += ' ';
sString.append('2');
cout << sString << endl;
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)
Sample code:
string sString("aaa");
sString.append(4, 'b');
cout << sString << endl;
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)
|
17.7 — std::string inserting
|
Index
|
17.5 — std::string assignment and swapping
|
17.7 — std::string inserting
Index
17.5 — std::string assignment and swapping
[...] 17.6 — std::string appending [...]
[...] 17.6 — std::string appending Print This Post This entry is filed under C++ Programming, C++ Tutorial. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. No Responses to “17.7 — std::string inserting” [...]
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
[...] 17.6 — std::string appending ch_client = "soorajchettada"; ch_width = 468; ch_height = 60; ch_type = "mpu"; ch_sid = "Chitika+Default"; ch_backfill = 1; ch_color_site_link = "#128A00"; ch_color_title = "#128A00"; ch_color_border = "#FFFFFF"; ch_color_text = "#000000"; ch_color_bg = "#FFFFFF"; Tags: c++, C++ Programming, C++ Tutorial, code string, index returns, length error, programming, tutorial, type index, type num Comments (0) Trackbacks (0) Leave a comment Trackback [...]
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
[...] assign() +=, append(), push_back() insert() clear() erase() replace() resize() [...]