Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

C++ Standard Library Strings: std::string and string_view

C++
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 24, 2023
UpdatedNov 17, 2026
6 min read

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.

Table of Contents

  • The string types in the standard library
  • Creating, concatenating and comparing strings
  • Accessing and modifying characters
  • Searching and splitting text
  • Converting between strings and numbers
  • Avoiding copies with std::string_view
  • Frequently Asked Questions
    • Is std::string null terminated?
    • What is the difference between std::string and std::string_view?
    • Does std::string support Unicode?
    • How do I convert a string to uppercase?
  • Summary

The string types in the standard library

std::basic_string<CharT> is the class template, and the standard provides these aliases.

TypeCharacter typeTypical use
std::stringcharText in UTF-8 or the system encoding
std::wstringwchar_tPlatform specific wide text
std::u8stringchar8_t (C++20)UTF-8 encoded text
std::u16stringchar16_tUTF-16 code units
std::u32stringchar32_tUTF-32 code points
std::string_viewcharNon-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.

Creating, concatenating and comparing strings

cpp
text

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.

Accessing and modifying characters

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.

cpp
text

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.

Searching and splitting text

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.

cpp
text

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>.

Converting between strings and numbers

Avoid atoi and sprintf. Use these instead.

  1. std::stoi, std::stod and friends throw on invalid input.
  2. std::to_string converts numbers to strings.
  3. std::from_chars and std::to_chars (C++17, <charconv>) are fast, locale independent and do not allocate or throw.
  4. std::format (C++20, <format>) replaces printf-style formatting with type-safe output. Compiler and library support is now common, but check your toolchain.
cpp
text

Avoiding copies with std::string_view

A 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.

Frequently Asked Questions

Is 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.

What is the difference between 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.

Does 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.

How do I convert a string to uppercase?

Use std::transform with std::toupper and cast the argument to unsigned char. This works for ASCII; full Unicode case mapping needs a library.

Summary

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.

Share