字符串查找 java 字符串查找与替换头歌_bc


查找

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

//查找
void test01()
{
	string str1= "abcdefgde";
	int pos=str1.find("cd");

	if(pos == -1)
	{
		cout<<"未找到字符串"<<endl;
	}
	else
	{
		cout<<"找到,pos = "<<pos<<endl;
	}

	//rfind与find的查找:rfind是从右开始查找,find是从左开始查找
	 pos = str1.rfind("cd");
	 cout<<"pos = "<<pos<<endl;

}

int main()
{
	test01();
	return 0;
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722215110293.png)
替换

```cpp
void test02()
{
	string str1 = "abcdefg";
	str1.replace(1,3,"1234");  //从1号下标开始起3三个字符的位置替换为“1234”
	cout<<"str1   "<<str1<<endl;

}

字符串查找 java 字符串查找与替换头歌_#include_02