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

 

你可能感兴趣的:(string::substr()函数调用)