C++中substr函数的用法

 

声明如下:

string substr (size_t pos = 0, size_t len = npos) const;

第一个参数是起始位置,第二个参数是偏移量。

 

#include<string>
#include<iostream>
using namespace std;

main()
{
string s="1234567890";
string a=s.substr(0,5);       //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾
cout<<a<<endl;
}

输出结果为:

12345

posted @ 2017-04-27 18:47  悟空的爸爸  阅读(285)  评论(0编辑  收藏  举报