Altering strings

The string classes have standard container access methods, so you can access individual characters through a reference (read and write access) with the at method and [] operator. You can replace the entire string using the assign method, or swap the contents of two string objects with the swap method. Further, you can insert characters in specified places with the insert method, remove specified characters with the erase method, and remove all characters with the clear method. The class also allows you to push characters to the end of the string (and remove the last character) with the push_back and pop_back methods:

    string str = "hello";     cout << str << "n"; // hello     str.push_back('!');  cout << str << "n"; // hello!  ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.