1、替换字符串s1中的s2.
#include <iostream>
using namespace std;
#include "string"
#include "algorithm"
void main()
{
string s1 = "STL可分为容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)六个部分";
string s2 = "、";
int len = s2.length();
cout << len << endl;
cout << s1<< endl;
int off = 0;
off = s1.find(s2, 0);
while (off != string:: npos)
{
s1.replace(off, len ,"123");
off = s1.find(s2, off + 1);
}
cout << "s1替换后的结果: " << s1 << endl;
system("pause");
}
2、去除字符串中的括号
using namespace std;
void main()
{
string s1 = "STL可分为容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)六个部分";
string s2 = "(";
string s3 = ")";
int len = s2.length();
cout << len << endl;
cout << s1<< endl;
int off = 0;
off = s1.find(s2, 0);
while (off != string:: npos)
{
s1.erase(off, len);
//s1.replace(off, len ,"123");
off = s1.find(s2, off + 1);
}
off = s1.find(s3, 0);
while (off != string::npos)
{
s1.erase(off, len);
//s1.replace(off, len ,"123");
off = s1.find(s3, off + 1);
}
cout << "\ns1替换后的结果: " << s1 << endl;
system("pause");
}
3.小写字母替换为大写
#include <iostream>
using namespace std;
#include "string"
#include "algorithm"
void main()
{
string s1 = "STL可分为容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)六个部分";
cout << "s1: " << s1 << endl;
transform(s1.begin(), s1.end(), s1.begin(), toupper);
cout << "\ns1替换后的结果: " << s1 << endl;
system("pause");
}