#include<iostream>
using namespace std;
//1.查找
void test0401()
{
string str1 = "abcdefgde";
int pos1 = str1.find("de");
cout << "pos1=" << pos1 << endl;
int pos2 = str1.find("df");//-1未找到字符串
cout << "pos2=" << pos2 << endl;
//rfind和find的区别
//1.rfind从右往左找, find从左往右找第一个匹配的字符串
int pos3= str1.rfind("de");
cout << "pos3=" << pos3 << endl;
}
//2.替换
void test0402()
{
string str1 = "abcdefg";
str1.replace(1, 3, "1111");//从1号位置起三个字符替换为“1111”
cout << "str1=" << str1 << endl;
}
int main()
{
//test0401();
test0402();
system("pause");
return 0;
}
网友评论