string::substr()简介

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

 

posted on 2012-07-20 16:24  NLP新手  阅读(27788)  评论(1编辑  收藏  举报

导航