Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ std::string_view operations

Reference for the std::string_view member functions: size, substr, find, compare, remove_prefix, starts_with, contains, copy and the rules that keep views safe.

std::string_view has a read-only interface that mirrors the non-modifying part of std::string: size queries, element access, substr, the find family, comparison and, since C++20/23, starts_with, ends_with and contains. The two functions that change a view are remove_prefix and remove_suffix, and they only move the view's edges: the characters themselves are never touched.

Because every operation works on a pointer and a length, slicing a view is free. That makes string_view a good fit for tokenizers and parsers that walk through a large text without allocating a single substring.

C++
std::string_view sv = "key=value";
auto eq = sv.find('=');
std::string_view key = sv.substr(0, eq);   // "key", no copy

Try it

C++ Splitting text without copying

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Trimming, searching and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
constexpr size_type size() const noexcept;           // also length()
constexpr bool empty() const noexcept;
constexpr const_reference operator[](size_type pos) const;
constexpr const_reference at(size_type pos) const;
constexpr const_reference front() const;
constexpr const_reference back() const;
constexpr const_pointer data() const noexcept;

constexpr void remove_prefix(size_type n);
constexpr void remove_suffix(size_type n);
constexpr void swap(basic_string_view& v) noexcept;

constexpr basic_string_view substr(size_type pos = 0, size_type count = npos) const;
constexpr size_type copy(CharT* dest, size_type count, size_type pos = 0) const;

constexpr size_type find(basic_string_view v, size_type pos = 0) const noexcept;
constexpr int compare(basic_string_view v) const noexcept;
constexpr bool starts_with(basic_string_view sv) const noexcept;   // C++20
constexpr bool contains(basic_string_view sv) const noexcept;      // C++23
Name Description
pos Zero-based index of the first character to look at.
count Number of characters to take or copy. Values past the end are clamped.
n Number of characters to drop in remove_prefix / remove_suffix.
v, sv The text to search for or compare with. Each of these also has overloads for a single char and for a C string.

Returns: find, rfind and the find_*_of functions return the index of the match or std::string_view::npos. compare returns a negative number, zero or a positive number. substr returns a new view of the same storage.

Errors / undefined behavior: at() throws std::out_of_range for an invalid index and substr(pos) and copy throw it when pos > size(). operator[], front() and back() do no checking: using them on an empty view or past the end is undefined behavior, as is remove_prefix(n) or remove_suffix(n) with n > size().

Usage notes

  • Cheap slicing. substr does not copy. The result points into the same characters, so it is valid only as long as the original storage is.
  • Not null-terminated. data() of a slice does not end at the slice's end. Never pass it to a function expecting a C string, and convert to std::string first when you need a terminator: std::string(sv).
  • npos arithmetic. find returning npos and then using it in pos + 1 or substr(npos) is the most common bug. Check against npos first; the trimming example above clamps with std::min.
  • Only remove_* mutate the view, never the text. Mutating the underlying buffer while a view of it exists can leave the view looking at different characters.
  • Comparison is lexicographic by character value. Use == and <=> with other views, strings and string literals; mixed comparisons work thanks to implicit conversions.
  • Complexity. Accessors, substr, remove_* are O(1). find, compare, copy are linear in the lengths involved.
  • Version notes. starts_with and ends_with arrived in C++20, contains in C++23. Before that use sv.substr(0, n) == prefix or find(...) != npos.
  • Literals. "text"sv (with using namespace std::literals;) creates a view at compile time and knows the exact length, including embedded '\0' characters.