From cppreference.com
constexprvoidremove_suffix(size_typen);(since C++17)Moves the end of the view back by n characters.
If n>size() is true, the behavior is undefined.
(until C++26)If n>size() is true:
If the implementation is
, a
occurs.
If the implementation is not hardened, the behavior is undefined.
(since C++26)Parameters
n - number of characters to remove from the end of the view Complexity
Constant.
Example
Run this code
#include<iostream>#include<string_view>intmain(){chararr[]={'a','b','c','d','\0','\0','\0'};std::string_viewv(arr,sizeofarr);autotrim_pos=v.find('\0');if(trim_pos!=v.npos)v.remove_suffix(v.size()-trim_pos);std::cout<<"Array: '"<<arr<<"', size="<<sizeofarr<<'\n'<<"View : '"<<v<<"', size="<<v.size()<<'\n';}Output:
Array: 'abcd', size=7 View : 'abcd', size=4 See also