Header Reference: <string>
Header Reference: <string>
The main string types, common operations, and usage patterns around std::basic_string and std::string.
Header Reference: <string>
The main string types, common operations, and usage patterns around std::basic_string and std::string.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.
<string>std::basic_string<CharT, Traits, Allocator>std::string, std::wstring, std::u8string, std::u16string, std::u32stringsize, capacity, reserve, shrink_to_fit, data, c_strappend, push_back, insert, erase, replace, clear, resize, swapfind, rfind, substr, starts_with, ends_with, contains, compareappend, push_back, insert, erase, replacefind, rfind, substr, starts_with, ends_with, containssize, empty, data, c_strstd::string_viewdata() and c_str() expose contiguous character storage suitable for interop with APIs expecting C-style stringsnpos on failure rather than throwingstd::string is a byte container for characters; it does not solve Unicode text segmentation or encoding policy by itself#include <string>
#include <string_view>
bool is_config_name(std::string_view text) {
return text.ends_with(".toml");
}
int main() {
std::string file = "config.toml";
file.insert(0, "site-");
return is_config_name(file) ? 0 : 1;
}
<string_view> for borrowed read-only parameters<format> or <print> for output formatting<charconv> for low-level conversions#include <string>
#include <iostream>
int main() {
std::string name = "cpp-handbook";
name.replace(0, 3, "C++");
if (name.starts_with("C++")) {
std::cout << name.substr(0, 7) << '\n';
}
}