0
点赞
收藏
分享

微信扫一扫

192_string容器_字符串查找和替换

李雨喵 2022-05-02 阅读 70

在这里插入图片描述

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

// 字符串查找和替换
void test01()
{
	string str1 = "abcdefg";
	int pos = str1.find("df");

	if (pos == -1)
	{
		cout << "cannot find string" << endl;
	}
	else {
		cout << " pos = " << pos << endl;
	}

	//rfind
	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;
	
}


// replace
void test02()
{
	string str1 = "abcdefg";
	str1.replace(1, 3, "1111");
	cout << "str1 = " << str1 << endl;
}

int main()
{	
	test02();
}

举报

相关推荐

0 条评论