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

2019-10-1216:48:25来源: 淼淼1111 评论 1,553 views
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string x="Hello_World";
    /*
    默认截取从0到npos.重载原型为string substr(_off=0,_count=npos);
    npos一般表示为string类中不存在的位置,_off表示字符串的开始位置,_count截取的字符的数目
    */
    cout<<x.substr()<<endl;
    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>源字符串的长度,则子字符串将延续到源字符串的结尾
    */
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: