Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ string::substr

Extract part of a std::string with substr(pos, count): default arguments, out_of_range rules, copies versus string_view slices and common splitting patterns.

substr(pos, count) returns a new std::string holding up to count characters starting at index pos. The count defaults to npos, meaning "to the end". The original string is unchanged, and the result is an independent copy.

Because the result is a copy, substr costs O(count) and may allocate. When you only need to read a slice, std::string_view::substr returns a view without copying, as long as the original string outlives it.

C++
std::string s = "hello world";
s.substr(6);       // "world"
s.substr(0, 5);    // "hello"
s.substr(3, 100);  // "lo world": count is clamped

Try it

C++ Taking pieces

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++ Exceptions and splitting

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++
basic_string substr(size_type pos = 0, size_type count = npos) const;        // C++11
basic_string substr(size_type pos = 0, size_type count = npos) const&;       // C++23
basic_string substr(size_type pos = 0, size_type count = npos) &&;           // C++23
Name Description
pos Index of the first character to copy. Must be <= size().
count Maximum number of characters; the real length is min(count, size() - pos).

Returns: a new string containing the selected characters.

Errors: throws std::out_of_range if pos > size(). pos == size() is allowed and returns an empty string.

Usage notes

  • Only pos is checked. An oversized count is silently reduced, so s.substr(0, 1000) is safe.
  • It is a copy. Edits to the result do not affect the source. In C++23, calling substr on an rvalue string reuses its buffer, so std::move(s).substr(n) avoids an extra allocation.
  • Prefer views for read-only slices. std::string_view(s).substr(a, b) does not allocate; just keep the string alive and unchanged while you use the view.
  • Do not build with s.substr(i, 1) in loops to get single characters: use s[i] or s.at(i).
  • Negative positions do not exist. Passing -1 converts to a huge unsigned value and throws. Compute from the end with s.size() - n after checking n <= s.size().
  • Alternatives: the constructor std::string(s, pos, count) does the same job; the iterator constructor std::string(s.begin() + a, s.begin() + b) never throws for valid iterators.
  • Equality helpers: starts_with and ends_with (C++20) replace s.substr(0, n) == prefix.