The C++ Standard Library handles text with std::basic_string, whose common instantiation is std::string (a sequence of char), declared in the <string> header. It manages its own memory, grows on demand, supports concatenation, comparison, searching and slicing, and works with iterators and standard algorithms. For read-only views of text, C++17 added std::string_view, and C++20 and C++23 added std::format and more string utilities.
This guide covers the string types, the operations you use every day, and the modern tools that replace most char* and sprintf code.
std::string_viewstd::basic_string<CharT> is the class template, and the standard provides these aliases.
| Type | Character type | Typical use |
|---|---|---|
std::string | char | Text in UTF-8 or the system encoding |
std::wstring | wchar_t | Platform specific wide text |
std::u8string | char8_t (C++20) | UTF-8 encoded text |
std::u16string | char16_t | UTF-16 code units |
std::u32string | char32_t | UTF-32 code points |
std::string_view | char | Non-owning, read-only view (C++17) |
std::string stores bytes, not user-visible characters. size() returns the number of char elements, so a UTF-8 string containing accented letters has more bytes than letters. Use a library such as ICU if you need Unicode-aware operations.
Comparison operators compare character by character. In C++20 the three-way operator <=> is also available. Use empty() to test for an empty string instead of comparing the size yourself.
Use operator[] for unchecked access and at() for bounds-checked access that throws std::out_of_range. front() and back() return the first and last characters, and data() or c_str() give a null-terminated const char* for C APIs.
substr(pos, count) returns a copy and throws std::out_of_range when pos is greater than size(). Methods such as reserve() avoid repeated reallocation when you know the final size in advance.
find, rfind, find_first_of and related functions return a position or std::string::npos when nothing matches. Since C++20 there are also starts_with, ends_with and, in C++23, contains.
The standard library has no single split function for plain strings. Use find in a loop, or in C++20 use std::views::split from <ranges>.
Avoid atoi and sprintf. Use these instead.
std::stoi, std::stod and friends throw on invalid input.std::to_string converts numbers to strings.std::from_chars and std::to_chars (C++17, <charconv>) are fast, locale independent and do not allocate or throw.std::format (C++20, <format>) replaces printf-style formatting with type-safe output. Compiler and library support is now common, but check your toolchain.std::string_viewA std::string_view is a pointer plus a length. Taking std::string_view instead of const std::string& lets a function accept string literals, std::string objects and substrings without allocating. The view does not own the data, so the underlying string must outlive it, and you should never return a view to a local string.
Also note that small strings are usually stored inside the object itself (the small string optimisation), so short strings often need no heap allocation. This is an implementation detail, not a guarantee.
std::string null terminated?c_str() and data() always return a null-terminated array since C++11. The string can still contain embedded '\0' characters, and size() counts them.
std::string and std::string_view?std::string owns and can modify its characters. std::string_view only refers to characters owned elsewhere, is cheap to copy and is read-only.
std::string support Unicode?It stores bytes, so it can hold UTF-8, but its functions work on bytes rather than characters. Use std::u8string or a Unicode library for character-aware work.
Use std::transform with std::toupper and cast the argument to unsigned char. This works for ASCII; full Unicode case mapping needs a library.
Use std::string for owned text, std::string_view for read-only parameters, std::format for formatting and std::from_chars for fast parsing. Prefer at() when indices are untrusted and remember that strings are byte sequences, not Unicode characters.