Strings library
Owning strings, string views, character traits, literals, conversions, and adjacent text/encoding routes.
The C++ strings area spans owning string containers, non-owning views, character traits, literals, null-terminated compatibility APIs, and newer text-encoding adjacent facilities. This page is the curated starting point when you know your problem is “text-like”, but you still need the right model.
# Start Here
Owning strings
Use std::basic_string and its aliases when the string owns its storage, needs mutation, or must outlive the source buffer it came from.
Non-owning views
Use std::basic_string_view when you want to read from an existing character sequence without copying it and can guarantee the referenced storage stays alive.
String literals and character kinds
Start here when the issue is about literal syntax, prefixes, encodings, or choosing between char, wchar_t, char8_t, char16_t, and char32_t.
String literals · Character sets and encodings · text_encoding
Null-terminated compatibility APIs
Use the byte, multibyte, and wide-string families when you need C-compatible interfaces, locale-sensitive conversions, or legacy NTBS-style algorithms.
# Quick Map
| If you need to | Start with | Why |
|---|---|---|
| Store and mutate text with owned lifetime | basic_string | It owns storage, supports mutation, growth, searching, replacement, and the standard string aliases such as std::string and std::u8string. |
| Pass read-only text around without copying | basic_string_view | It models a lightweight non-owning view over contiguous characters, but the caller must manage lifetime carefully. |
| Convert numbers to or from strings | to_string, stoi/stol/stoll, stof/stod/stold | These are the string-focused conversion helpers; use to_chars and from_chars for lower-level locale-independent conversion. |
| Choose the right literal form or suffix | String literals, basic_string, basic_string_view | Literals determine character type, encoding form, and whether the result is an array, an owning string, or a string_view. |
| Work with C-compatible null-terminated strings | Byte strings, Wide strings, Multibyte | These sections hold the NTBS-style and locale-sensitive APIs that remain important for interoperability and lower-level text handling. |
| Understand encodings, character traits, or text model boundaries | char_traits, Character sets, text_encoding | These pages explain how character operations are abstracted and how encoding-aware code relates to literal and runtime environments. |
# Core String Models
| Model | Use it when | Primary destinations |
|---|---|---|
| Owning dynamic string | You need allocation, mutation, append/replace operations, or independent lifetime. | basic_string, constructors, concatenation |
| Non-owning string view | You need a cheap parameter or slice type over existing text and can guarantee the source buffer remains valid. | basic_string_view, substr, remove_prefix |
| Character operation policy | You need generic character comparison/copy/length behavior for a string-like type. | char_traits |
| C-compatible null-terminated strings | You are interoperating with legacy APIs, locale conversion routines, or NTBS-style algorithms. | byte, multibyte, wide |
# Character Types And Encodings
Literal forms
String literal prefixes choose the code unit type and affect whether you start from ordinary, wide, UTF-8, UTF-16, or UTF-32 literal data.
language literals · basic_string aliases · string_view aliases
Character sets
Use the language charset page when the question is about source, execution, ordinary, or wide literal encodings rather than a particular string container.
Runtime encoding identity
std::text_encoding is the modern route for reasoning about literal, locale, and environment encoding identities at compile time and runtime.
# Conversion And Formatting Routes
| Task | Preferred route | Related route |
|---|---|---|
| Numeric value to string | to_string, to_wstring | format for richer formatting, to_chars for lower-level conversion |
| String to integer | stoi/stol/stoll, stoul/stoull | from_chars when you want locale-independent parsing with tighter control |
| String to floating-point | stof/stod/stold | from_chars for low-level parsing paths |
| Stream a string-like object | basic_stringstream, stream operators | I/O hub for broader stream model choices |
# Version Highlights
| Standard | What changed navigation-wise |
|---|---|
| C++11 | User-defined string literal suffixes, new conversion helpers like to_string, and the UTF-16/UTF-32 character families became part of the main modern string story. |
| C++17 | string_view became the default non-owning route, which materially changes API design and parameter-passing choices. |
| C++20 | char8_t and UTF-8-specific aliases became first-class, making the choice of character type and literal prefix more visible than before. |
| C++23 | More string/view convenience APIs such as contains and ranges-aware member additions reduce the need for ad hoc helper code. |
| C++26 | text_encoding gives the strings/text area a clearer runtime encoding-identification entry point, and to_string aligns more closely with modern formatting behavior. |
# Boundary Lines
| This hub covers | Use a different hub for |
|---|---|
| Owning strings, string views, traits, literals, NTBS compatibility, and string-specific conversions. | Text for the dedicated text-encoding surface, I/O for stream architecture, Utility for formatting and char-conversion primitives, Locale for locale facets and localization behavior. |
| C++ string abstractions over character sequences. | C string when you need the C-side library surface or cross-language compatibility details. |