std::basic_string<CharT,Traits,Allocator>::subview - cppreference.com

From cppreference.com

constexprstd::basic_string_view<CharT,Traits>subview(size_typepos=0,size_typecount=npos)const;(since C++26)Returns a view of the substring [pos, pos+rlen), where rlen is the smaller of count and size()-pos.

Equivalent to returnstd::basic_string_view<CharT,Traits>(*this).subview(pos,count);.

Parameters

pos - position of the first character to include count - length of the sub-view Return value

View of the substring [pos, pos+rlen).

Exceptions

std::out_of_range

if pos>size().

Complexity

Constant.

Notes

Feature-test

macro ValueStdFeature

__cpp_lib_string_subview

202506L

(C++26)std::basic_string::subview, std::basic_string_view::subviewExample

Run this code

#include<cassert>#include<iostream>#include<string>#include<string_view>intmain(){conststd::strings{"Life is life!"};assert(s.subview(5)=="is life!");assert(s.subview(5,13)=="is life!");assert(s.subview(5,2)=="is");try{// pos is out of bounds, throwsconstautopos{s.length()+13};[[maybe_unused]]autox_x{s.subview(pos)};}catch(conststd::out_of_range&ex){std::cout<<"Exception: "<<ex.what()<<'\n';}}Possible output:

Exception: basic_string_view::substr: __pos (which is 26) > __size (which is 13) See also

returns a substring
(public member function)

[edit]

(C++26)

returns a sub-view
(public member function of std::basic_string_view<CharT,Traits>)

[edit]