【C++】string::substr函数

形式:s.substr(p, n)

返回一个string,包含字符串s中从p开始的n个字符的拷贝(p的默认值是0,n的默认值是s.size() - p,即不加参数会默认拷贝整个s)

 1 int main()
 2 {
 3     string a;
 4     string s("123456789");
 5     
 6     a = s.substr(0,5);//拷贝字符串s中从第0位开始的长度为5的字符串
 7     cout << a << endl;//输出12345
 8     
 9     a=s.substr(); //不加参数即默认拷贝整个字符串s
10     cout<<a<<endl;//输出123456789
11     
12     a=s.substr(4);//输出56789
13     cout<<a<<endl;//单个参数则默认拷贝从第4位开始至末尾的字符串
14 }

猜你喜欢

转载自www.cnblogs.com/HOLLAY/p/11324452.html