注意:其中第一个参数为index,第二个参数为偏移量,并不是索引值
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("12345abcd");
string a=s.substr(0,5); //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾
string b=s.substr(5,3); //获得字符串s中 从第5位开始的长度为3的字符串//
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
输出结果为:
12345
abc