用 string 进行插入、替代、查找输出下标等操作

时间:2023-03-09 05:33:02
用 string 进行插入、替代、查找输出下标等操作
 string s;
s = "";
string::iterator it;
it = s.begin();//让s指向第一个元素
cout << s;
system("pause");
return ;

在未进行插入之前的运行结果:

用 string 进行插入、替代、查找输出下标等操作

进行插入操作后运行结果,在字符串第二个元素后面进行插入元素 ‘3’ 操作:

 string::iterator it;
it = s.begin();
s.insert(it + , '');

运行结果如下所示:

用 string 进行插入、替代、查找输出下标等操作

替代操作:

将初始化的数组从第三个下标开始的元素连续4个替代

 s.replace(, , "good");

未替代前运行结果:

用 string 进行插入、替代、查找输出下标等操作

替代后运行结果:

用 string 进行插入、替代、查找输出下标等操作

查找操作:

初始化:

 string s;
s = "I am people";

查找 a 元素 单个元素并输出下标:

  s.find('a');
cout << s.find('a');

运行结果:

用 string 进行插入、替代、查找输出下标等操作

中间有空格影响:

把空格去了:

初始化:

 s = "Iampeople";
cout << s<<endl;

输出结果:

用 string 进行插入、替代、查找输出下标等操作

查找people 单词:

初始化:

string s;
s = "I am people";
cout << s<<endl;

查找

  s.find("people");
cout << s.find("people");

运行结果:

用 string 进行插入、替代、查找输出下标等操作

不知道为什么,打出的只是首个元素的下标,并且系统自动把空格当一个元素处理了