std::string is a growable, owning sequence of char with value semantics — copying a string copies its characters. Forget char* for text.
Create, combine, access
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string a = "Hello";
std::string b(3, '!'); // "!!!"
std::string c = a + ", world" + b; // concatenation
c += '?';
std::cout << c << ' ' << c.size() << '\n';
std::cout << c[0] << c.front() << c.back() << ' ' << c.at(4) << '\n';
std::cout << std::boolalpha << c.empty() << ' ' << (a == "Hello") << ' ' << (a < "Help") << '\n';
for (char& ch : a) ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
std::cout << a << '\n';
}Hello, world!!!? 16
HH? o
false true true
HELLOSearch & modify
#include <iostream>
#include <string>
int main() {
std::string s = "the quick brown fox";
std::cout << s.find("quick") << ' ' << s.find('o') << ' ' << s.rfind('o') << '\n';
if (s.find("cat") == std::string::npos) std::cout << "no cat\n";
std::cout << s.substr(4, 5) << '|' << s.substr(16) << '\n'; // substr(pos, count)
s.replace(4, 5, "slow"); // replace(pos, count, text)
s.insert(0, ">> ");
s.erase(s.size() - 4); // erase from pos to end
std::cout << s << '\n';
std::cout << s.starts_with(">>") << s.ends_with("brown") << '\n'; // C++20
}4 12 17
no cat
quick|fox
>> the slow brown
11| Operation | Call | Notes |
|---|---|---|
| Length | s.size() / s.length() | O(1) |
| Find | s.find(x, pos), rfind, find_first_of | Returns index or std::string::npos |
| Substring | s.substr(pos, len) | Returns a new string (copy) |
| Modify | +=, push_back, pop_back, insert, erase, replace, clear | Amortised O(1) append |
| Compare | ==, <, s.compare(t) | Lexicographic |
| Prefix/suffix | starts_with, ends_with (C++20), contains (C++23) | |
| C string | s.c_str() | Null-terminated const char* for C APIs |
Numbers ↔ strings
#include <iostream>
#include <string>
int main() {
int n = std::stoi("42");
double d = std::stod("3.5");
long long big = std::stoll("9000000000");
std::string s = std::to_string(n + 1) + "/" + std::to_string(big);
std::cout << n * 2 << ' ' << d * 2 << ' ' << s << '\n';
std::cout << std::stoi("ff", nullptr, 16) << ' ' << std::stoi(" 12abc") << '\n'; // base 16; stops at non-digits
try { std::cout << std::stoi("abc"); } catch (const std::invalid_argument&) { std::cout << "invalid\n"; }
}84 7 43/9000000000
255 12
invalidSplitting and joining
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> split(const std::string& s, char delim) {
std::vector<std::string> parts;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) parts.push_back(item);
return parts;
}
int main() {
auto parts = split("red,green,,blue", ',');
std::cout << parts.size() << '\n';
std::string joined;
for (std::size_t i = 0; i < parts.size(); ++i) joined += (i ? " | " : "") + parts[i];
std::cout << joined << '\n';
}4
red | green | | blueWhat does std::string::npos mean?
Is std::string null-terminated?