std::basic_string<CharT,Traits,Allocator>::substr_C++中文网

basic_string substr( size_type pos = 0, size_type count = npos ) const;

(C++20 前)

constexpr basic_string substr( size_type pos = 0, size_type count = npos ) const;

(C++20 起)

返回子串 [pos, pos+count) 。若请求的子串越过 string 的结尾,或若 count == npos ,则返回的子串为 [pos, size())

参数

pos - 要包含的首个字符的位置
count - 子串的长度

返回值

含子串 [pos, pos+count) 的 string 。

异常

pos > size() 则为 std::out_of_range

复杂度

count 成线性

注解

如同以 basic_string(data()+pos, count) 构造返回的 string ,这隐含将会默认构造返回的 string 的分配器——新分配器将this->get_allocator() 的副本。

示例

输出:

abcdefghij
567
hij
pos exceeds string size

参阅