string::substr()简介

std::basic_string::substr

C++Strings librarystd::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 ifcount==npos, the returned substring is[pos, size()).

Parameters

posposition of the first character to include

countlength of the substring

Return value

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

Exceptions

std::out_of_rangeifpos>size().

Complexity

Linear incount

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/archive/2012/07/20/2601328.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/56082

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午7:04
下一篇 2023年2月9日 上午7:04

相关推荐