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
**************************************************************
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对困难,能够不休不眠;面对压力,能够迎接挑战。他们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想,用智慧把属于自己的事业开创。其实我是一个程序员
[=.=]
原文地址:https://www.cnblogs.com/kevinGaoblog/p/2601328.html