C++ string.substr

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

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

string b=s1.substr(4);   //  s1.substr(pos, n)包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s) 这个意思就是从4的位置开始截取字符串(9-4)

}

输出结果为:

1234

56789

猜你喜欢

转载自blog.csdn.net/single6/article/details/81946554