C++ string 类中substr的使用方法

来源:互联网 发布:linux切换目录命令 编辑:程序博客网 时间:2024/05/08 17:11

#include<iostream>
#include<string>
using namespace std;
int main()
{
string x="Hello_World";
cout<<x.substr()<<endl;/*默认截取从0到npos.重载原型为string substr(_off=0,_count=npos);npos一般表示为string类中不存在的位置,_off表示字符串的开始位置,_count截取的字符的数目*/
cout<<x.substr(5)<<endl;//截取x[5]到结尾,即npos.重载原型为string substr(_off,_count=npos)
cout<<x.substr(0,5)<<endl;//以x[0]为始,向后截取5位(包含x[0]),重载原型string substr(_off,_count)
/*
备注:
指定的截取长度加起始位置即_off+_count>源字符串的长度,则子字符串将延续到源字符串的结尾
*/
}


1 0
原创粉丝点击