string::substr()简介

痴心易碎 提交于 2020-02-13 03:47:09

std::basic_string::substr

C++
Strings library
std::basic_string

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

Returns a substring [pos, pos+count). If the requested substring lasts past the end of the string, or if count == npos, the returned substring is [pos, size()).

Parameters

pos   position of the first character to include

count  length of the substring

Return value

String containing the substring [pos, pos+count).

Exceptions

std::out_of_range if pos > size().

Complexity

Linear in count

Example

 1 #include <string>
 2 #include <iostream>
 3  
 4 int main()
 5 {
 6     std::string a = "0123456789abcdefghij";
 7  
 8     std::string sub1 = a.substr(10);
 9     std::cout << sub1 << '\n';
10  
11     std::string sub2 = a.substr(5, 3);
12     std::cout << sub2 << '\n';
13  
14     std::string sub3 = a.substr(12, 100);
15     std::cout << sub3 << '\n';
16  }

Output:

1 abcdefghij
2 567
3 cdefghij

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!