C++之string类

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

int main()
{
        char *s1="first";
        char s2[]="second";
        string s3="third";
        string s4="forth";
        char ch='@';
        //concatenate
        string s5=s3+s4;
        cout<<s5<<endl;
        string s6=s3+s2;
        cout<<s6<<endl;
        string s7=s3+ch;
        cout<<s7<<endl;
        string s8,s9,s10;
        //insert
        s8=s9="123456";
        s10="aaa";
        s8.insert(3,s10);
        cout<<s8<<endl;
        s9.insert(3,"bbb");
        cout<<s9<<endl;
        //erase
        s8.erase(3);
        s9.erase(3,1);
        cout<<s8<<endl<<s9<<endl;
        //tiquchu zichuan
        string s12="one two three two";
        string s13;
        s13=s12.substr(3,4);
        cout<<s13<<endl;
        //find
        string s14="two";
        int index=s12.find(s14,2);//ruguo 2 zhege canshu meixie,ze cong 0 kaishi chazhao
        if(index<s12.length())cout<<"found:"<<index<<endl;
        else cout<<"not found"<<endl;
        //rfind
        int index2=s12.rfind(s14,3);//zjihui chazhaodao 3 zhegeweizhi
        cout<<"index:"<<index2<<endl;
        //find_first_of
        int index3=s12.find_first_of(s14);
        cout<<"find_first_of index:"<<index3<<endl;
}

//TODO

posted @ 2020-07-02 16:40  njit-sam  阅读(161)  评论(0)    收藏  举报