C++ std::string subview() Function

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

Definition

In C++, std::string::subview() returns a std::string_view that refers to a slice of the string without copying (C++26).

If your compiler lacks subview, build a view with std::string_view(s).substr(pos, len) or std::string_view(s.data() + pos, len). 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 position and optional length (C++26).

C++
std::string_view v = s.subview(2, 3);
CallWhat it does
s.subview(pos)View from pos to the end.
s.subview(pos, len)View of up to len characters.

This tutorial documents the C++26 direction. Examples below also show the portable string_view approach so you can practice today.

Library declarations
C++
constexpr string_view subview(size_type pos = 0, size_type len = npos) const; // C++26

Parameters

ParameterDescription
posStart index into the string.
lenMaximum view length.

Return Value

Return ValueDescription
string_viewNon-owning view of the selected characters.

C++ Examples

Example 1 — Portable string_view Slice

Create a view of the middle without copying (works pre-C++26).

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

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

How It Works

  1. We point at index 4 for length 2.
  2. The view refers to "To" inside s.
  3. No separate owning string is allocated for the slice.

Example 2 — View a Prefix

View the first four characters.

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

int main() {
    std::string s = "Hello!";
    std::string_view v = std::string_view(s).substr(0, 5);
    std::cout << v << '\\n';
    return 0;
}

How It Works

  1. string_view(s) views the whole string.
  2. substr on the view selects the prefix.
  3. Printing shows Hello without copying into a new std::string.

Example 3 — Keep the String Alive

Use the view only while s lives.

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

int main() {
    std::string s = "safe";
    std::string_view v = s;
    std::cout << v << '\\n';
    return 0;
}

How It Works

  1. The view aliases s's buffer.
  2. We print while s is in scope.
  3. Returning v after s dies would be a bug.

Frequently Asked Questions

A C++26 member that returns a string_view of a string slice without copying.
Use std::string_view(s).substr(pos, len) or construct a view from data()+pos.
No. It is a non-owning view.
If the string is destroyed or reallocates its buffer.
substr returns a new owning std::string; subview/string_view does not own.

Next: swap()

Keep going through the string functions. Exchange contents with another string.

swap() 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.

9 people found this page helpful