cb19a_c++_只适合string类型的操作_提取_追加_替换

时间:2023-03-09 16:52:35
cb19a_c++_只适合string类型的操作_提取_追加_替换

*cb19a_c++_只适合string类型的操作_提取_追加_替换
三个substr重载函数-获取一个字符串的一部分
六个append重载函数-追加字符
十个replace重载函数-替换更换

重载函数越多,使用起来越灵活

欢迎讨论,相互学习。 txwtech@163.com

 /*cb19a_c++_只适合string类型的操作_提取_追加_替换
三个substr重载函数-获取一个字符串的一部分
六个append重载函数-追加字符
十个replace重载函数-替换更换 重载函数越多,使用起来越灵活 欢迎讨论,相互学习。 txwtech@163.com
*/
#include <iostream>
#include <string> using namespace std; int main()
{
string s("hello world");
string s2 = s.substr(, );//第6位开始,提取5个字符
cout << "第6位开始,提取5个字符: " << s2 << endl; s2 = s.substr();
cout << "第6位开始,所有字符: " << s2 << endl; s2 = s.substr();//
cout << "s2=s一个意思: " << s2 << endl; s = "c++ Primer";
s.append("3rd Ed.");
cout << "追加字符"<<s << endl;
s.insert(s.size(),"3rd Ed.");//
cout << "也是追加字符:" << s << endl; s.replace(, , "4th");
cout << "11位开始,替换3个" << s << endl; s.replace(, , "Fourth");
cout << "11为开始,超出3,都替换上去:" << endl; s = "c++ primer 3rd Ed.";
s.erase(, );
s.insert(, "Fourth");
cout << "先删除3位,再插入:" <<s<< endl; return ;
}