C++ std::string substr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::substr() builds a new string from part of the current one.

You choose a start index and an optional length. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Pass start position and optional count.

C++
std::string a = s.substr(2);
std::string b = s.substr(2, 3);
CallWhat it does
s.substr(pos)Copy from pos to the end.
s.substr(pos, len)Copy up to len characters starting at pos.

If pos + len passes the end, substr copies only what remains—no throw for a long len.

Library declarations
C++
string substr(size_type pos = 0, size_type len = npos) const;

Parameters

ParameterDescription
posStart index. Throws if pos > size().
lenMax characters to copy. Default is to the end.

Return Value

Return ValueDescription
stringA new string with the requested characters.

C++ Examples

Example 1 — Basic substr()

Take from index 5 to end.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello World";
    std::cout << s.substr(6) << '\\n';
    return 0;
}

How It Works

  1. Index 6 starts at 'W'.
  2. No length means “to the end”.
  3. New string is "World".

Example 2 — Fixed Length Slice

Copy three characters from the middle.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "CodeToFun";
    std::cout << s.substr(4, 2) << '\\n';
    return 0;
}

How It Works

  1. Start at index 4.
  2. Length 2 selects T and o.
  3. Printed slice is "To".

Example 3 — substr Throws

pos past size throws.

C++
#include <iostream>
#include <stdexcept>
#include <string>

int main() {
    std::string s = "ab";
    try {
        auto t = s.substr(5);
        std::cout << t << '\\n';
    } catch (const std::out_of_range&) {
        std::cout << "bad pos\\n";
    }
    return 0;
}

How It Works

  1. Size is only 2.
  2. pos 5 is out of range.
  3. catch prints a safe message.

Frequently Asked Questions

A new std::string containing the requested slice.
It copies until the end without throwing.
Throws std::out_of_range.
No. It returns a copy.
std::string_view, and subview in newer drafts (C++26).

Next: subview()

Keep going through the string functions. Return a string_view into a slice of this string (C++26).

subview() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful