string类成员函数的使用方法(三)

15.find
功能:查找字符或字符串
返回值:返回找到的字符或字符串的第一个字符位置

    //找单个字符
    string s1("hello word");
    int iPos1 = s1.find('e', 0);//第二个参数是一个偏移量,指定从哪里开始找
    //注意,返回的数组下标,从0开始计数的
    if (iPos1 != string::npos)
    {
        cout << "e -- " << iPos1 << endl; 
    }
    else
    {
        cout << "没有找到 e "<< endl;
    }

    //找子字符串
    string s2("hello word");
    int iPos2 = s1.find("llo", 0);//第二个参数是一个偏移量,指定从哪里开始找
    if (iPos1 != string::npos)
    {
        cout << "llo -- " << iPos2 << endl;
    }
    else
    {
        cout << "没有找到 llo " << endl;
    }

    //查找字符串的部分字符
    string s3("hello word");
    int iPos3 = s1.find("lle", 0,2);//第三个参数是匹配多少个
    if (iPos3 != string::npos)
    {
        cout << "ll -- " << iPos3 << endl;
    }
    else
    {
        cout << "没有找到 ll " << endl;
    }

    string s4("hello word");
    string s5("hello");
    int iPos4 = s1.find(s5,0);//第二个参数是指定从哪个位置开始找
    if (iPos4 != string::npos)
    {
        cout << "hello -- " << iPos4 << endl;
    }
    else
    {
        cout << "没有找到 hello " << endl;
    }

16.find_first_not_of
功能:查找不符合某些条件的第一次出现的位置

    //找单个字符
    string s1("hello word");
    int iPos1 = s1.find_first_not_of('e', 0);//第二个参数是一个偏移量,指定从哪里开始找
    //注意,返回的数组下标,从0开始计数的
    if (iPos1 != string::npos)
    {
        cout << "e -- " << iPos1 << endl; 
    }
    else
    {
        cout << "全部符合条件 "<< endl;
    }

    //找子字符串
    string s2("hello word");
    int iPos2 = s1.find_first_not_of("he", 0);//第二个参数是一个偏移量,指定从哪里开始找
    if (iPos1 != string::npos)
    {
        cout << "he -- " << iPos2 << endl;
    }
    else
    {
        cout << "全部符合条件 " << endl;
    }

    //查找字符串的部分字符
    string s3("hello word");
    int iPos3 = s1.find_first_not_of("hee", 0,2);//第三个参数是匹配多少个
    if (iPos3 != string::npos)
    {
        cout << "he -- " << iPos3 << endl;
    }
    else
    {
        cout << "全部符合条件" << endl;
    }

    string s4("hello word");
    string s5("hello");
    int iPos4 = s1.find_first_not_of(s5,0);//第二个参数是指定从哪个位置开始找
    if (iPos4 != string::npos)
    {
        cout << "hello -- " << iPos4 << endl;
    }
    else
    {
        cout << "全部符合条件" << endl;
    }

17.front
功能:返回第一个元素的地址

    string s("hello word");
    cout << s.front() << endl;

18.pop_back
功能:删除最后一个元素

    string s("hello word");
    s.pop_back();
    cout << s << endl;

19.push_back
功能:向尾部添加

    string s("hello word");
    s.push_back('A');
    cout << s << endl;

20.rbegin
功能:反向迭代器,返回最后一个元素的迭代器地址,可以从后往前面遍历
相当于最后一个元素就是开始

21.rend
功能:和rbegin相反

22.insert
功能:将一定元素或者一定数量的元素插入到指定位置

    //向指定位置插入字符串
    string s1("hello word");
    s1.insert(0, "hello ");
    cout << s1 << endl;

    //向指定位置插入字符串的指定个数
    string s2("hello word");
    s2.insert(0, "hello ",3);
    cout << s2 << endl;

    //向指定位置插入字符串对象
    string s3("hello word");
    string str1("hello ");
    s3.insert(0, str1);
    cout << s3 << endl;

    //向指定位置插入字符串对象的指定位置开始的n个字符
    string s4("hello word");
    string str2("hello ");
    s4.insert(0, "hello ",1,2);
    cout << s4 << endl;

    //向指定位置插入一定数量的字符
    string s5("hello word");
    s5.insert(0,5,'A');
    cout << s5 << endl;

    //利用迭代在指定位置插入
    string s6("hello word");
    s6.insert(s6.begin()+2,'a');
    cout << s6 << endl;

    //第一个参数是要在哪里插入,后面两个是一对迭代器
    string s7("hello word");
    string str3("hello ");
    s7.insert(s7.begin(),str3.begin(),str3.end());
    cout << s7 << endl;

    string s8("hello word");
    s8.insert(s8.begin(), 5, 'A');
    cout << s8 << endl;

    string s9("hello word");
    const char* p = "hello ";
    s9.insert(s9.begin(),p,p+6);
    cout << s9 << endl;

    string s10("hello word");
    string str4("hello");
    string::const_iterator const_iter_begin = str4.begin();
    string::const_iterator const_iter_end = str4.end();
    s10.insert(s10.begin(), const_iter_begin, const_iter_end);
    cout << s10 << endl;

23.resize
功能:重新设定大小

    string s("hello");
    s.resize(10); //重新设定大小
    cout << s << endl;
    s.resize(20, 'A');//重新设定大小并且赋值
    cout << s << endl;
    s.resize(5);
    cout << s << endl;
    s.resize(3);     //大小减小,后面的字符被删除
    cout << s << endl;

24.rfind
功能:从后面开始查找

25.swap
功能:交换两个字符串对象的内容

    string s1("hello");
    string s2("word");
    s1.swap(s2);
    cout << s1 << endl;
    cout << s2 << endl;

26.substr
功能:截取子字符串

    string s("hello");
    cout << s.substr(2) << endl; //输出为llo
    cout << s.substr(2, 2) << endl;//输出为lo

27.replace
功能:用字符或者字符串替换原来字符串的全部或者部分

    //第二个参数是最多被替换的字符个数
    string s1("hello");
    s1.replace(0, 1, "he");
    cout << s1 << endl;

    //最后一个参数是参数字符串的最大个数
    string s2("hello");
    s2.replace(0, 1, "ABCD",3);
    cout << s2 << endl;

    //最后两个分别是参数串的位置和个数
    string s3("hello");
    s3.replace(0, 1, "ABCDEF",2,4);
    cout << s3 << endl;

    //2个A
    string s4("hello");
    s4.replace(0, 1, 2,'A');
    cout << s4 << endl;

    string s5("hello");
    s5.replace(s5.begin()+2,s5.end() , "AB");
    cout << s5 << endl;

    string s6("hello");
    string str("AB");
    s6.replace(s6.begin() + 2, s6.end(), str);
    cout << s6 << endl;

    //利用一对迭代器替代
    string s7("hello");
    string str1("AB");
    s7.replace(s7.begin(), s7.end(), str1.begin(),str1.end());
    cout << s7 << endl;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值