Skip to content
Justice Tech School

C++ String Methods

std::string has size, empty, substr, find, replace, append, insert, erase and comparison. to_string and stoi convert between numbers and text.

Example
string s = "Hello, C++";
cout << s.size() << " " << s.substr(7) << " " << s.find("C++") << "\n";
s.replace(7, 3, "World");
s += "!";
cout << s << "\n";
cout << to_string(42) + "x" << " " << stoi("123") + 1 << "\n";

Result

10 C++ 7
Hello, World!
42x 124

Exercise

Text to int.

("12")

Quiz Test

Which finds a substring?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.