basic_string::substr
来自cppreference.com
basic_string substr( size_type index = 0, size_type count = npos ) const;
返回一个当前字符串的从给出的位置index长度为count的子串。如果请求的子串超过了字符串的末尾或者count参数被给定值为npos,当前字符串的剩余部分都会被返回。
目录 |
[编辑] 参数
index - 子串包含的第一个字符的位置
count - 请求的子串的长度
[编辑] 返回值
请求的子串
[编辑] 示例
#include <string> #include <iostream> int main() { std::string a = "0123456789abcdefghij"; std::string sub1 = a.substr(10); std::cout << sub1 << std::endl; std::string sub2 = a.substr(5, 3); std::cout << sub2 << std::endl; std::string sub3 = a.substr(12, 100); std::cout << sub3 << std::endl; return 0; }
输出:
abcdefghij
567
cdefghij
[编辑] 复杂度
相关于count的线性复杂度
[编辑] 参见
copy