C++ STL string string content modification and replacement

//Modify and replace string content
#include <iostream>
#include <string>
using namespace std;

intmain()
{
    string str1("123456");
    string str2("abcdefghijklmn");
    string str;
    //Initialize str with str1
    str.assign(str1);
    cout<<str<<endl;
    //Initialize str using the 3rd bit of str1 with a length of 3 substrings
    str.assign(str1,3,3);
    cout<<str<<endl;
    //Initialize str using the second bit of str1 to the end of the string
    str.assign(str1,2,str1.npos);
    cout<<str<<endl;
    //initialize str with five 'A's
    str.assign(5,'A');
    cout<<str<<endl;
    string::iterator itB;
    string::iterator itE;
    //Get the starting position of the string
    itB=str1.begin();
    //Get the pointer to the end of the string
    itE=str1.end();
    str.assign(itB,(--itE));
    cout<<str<<endl;
    str = str1;
    cout<<str<<endl;
    //delete element 3 and after
    str.erase(3);
    cout<<str<<endl;
    str.erase(str.begin(),str.end());
    cout<<":"<<str<<":"<<endl;
    // swap elements
    str.swap(str2);
    cout<<str<<endl;
    //insert element
    string A("ello World");
    string B("H");
    B.insert(1,A);
    cout<<B<<endl;
    //insert element
    A="ello";
    B="H";
    B.insert(1,"yanchy",3);
    cout<<"插入:"<<B<<endl;
    //insert element
    A="ello";
    B="H";
    B.insert(1,A,2,2);
    cout<<"插入:"<<B<<endl;
    //insert element
    A="ello";
    B="H";
    B.insert(1,5,'C');
    cout<<"插入:"<<B<<endl;
    // insert content
    A="ello";
    B="H";
    string::iterator it=B.begin()+1;
    const string::iterator itF=A.begin();
    const string::iterator itG=A.end();
    B.insert(it,itF,itG);
    cout<<"插入:"<<B<<endl;
    A="ello";
    B="H";
    cout<<"A= "<<A<<",B= "<<B<<endl;
    // Additional string skewer
    B.append(A);
    cout<<"追加:"<<B<<endl;
    A="ello";
    B="H";
    cout<<"A= "<<A<<",B= "<<B<<endl;
    B.append("12345",2);
    cout<<"追加:"<<B<<endl;
    A="ello";
    B="H";
    cout<<"A= "<<A<<",B= "<<B<<endl;
    B.append("12345",2,3);
    cout<<"追加:"<<B<<endl;
    A="ello";
    B="H";
    cout<<"A= "<<A<<",B= "<<B<<endl;
    B.append(10,'a');
    cout<<"追加:"<<B<<endl;
    A="ello";
    B="H";
    cout<<"A= "<<A<<",B= "<<B<<endl;
    B.append(A.begin(),A.end());
    cout<<"追加:"<<B<<endl;
    cin.get();
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325555198&siteId=291194637