C++,STL 017(2024.09.26)

内容:string的查找替换操作。

#include <iostream>
#include <string>

using namespace std;

/*
查找函数:
1.find函数和rfind函数有多个重载版本,以下是基础运用。
2.find从字符串开头,从左往右查找;
  rfind从字符串末尾,从右往左查找。
3.成功找到字符串则返回第一个字符的所在位置;
  找不到则返回值-1。
4.字符串开头位置为0。
*/
void test01()
{
    string str1 = "abcdddefgddd";

    int pos1 = str1.find("ddd"); // here
    cout << "pos1 = " << pos1 << endl;

    int pos2 = str1.rfind("ddd"); // here
    cout << "pos2 = " << pos2 << endl;
}

/*
替换函数:
1.replace在替换时,要指定从哪个位置开始,将多少个字符,替换成什么样的字符串。
2.替换后的字符串用 char*型 和 string型 均可
3.字符串开头位置为0。
*/
void test02()
{
    string str2 = "aaaaaa";
    str2.replace(1, 2, "bbbb"); // here,从str2的第1个位置开始 将2个字符 替换成新字符串
    cout << "str2 = " << str2 << endl;
}

int main()
{
    test01();

    test02();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值