/*
*用str替换指定字符串从起始位置pos开始长度为len的字符
*string& replace (size_t pos, size_t len, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空
cout << line << endl;
return 0;
}
/*
*用str替换 迭代器起始位置 和 结束位置 的字符
*string& replace (const_iterator i1, const_iterator i2, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符
cout << line << endl;
return 0;
}
/*
*用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
*string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
*/
int main()
{
string line = "this@ is@ a test string!";
string substr = "12345";
line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line
cout << line << endl;
return 0;
}
/*
*用str替换从指定位置0开始长度为5的字符串
*string& replace(size_t pos, size_t len, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串
cout << line << endl;
return 0;
}
/*
*用str替换从指定迭代器位置的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串
cout << line << endl;
return 0;
}
/*
*用s的前n个字符替换从开始位置pos长度为len的字符串
*string& replace(size_t pos, size_t len, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串
cout << line << endl;
return 0;
}
/*
*用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串
cout << line << endl;
return 0;
}
/*
*用重复n次的c字符替换从指定位置pos长度为len的内容
*string& replace (size_t pos, size_t len, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容
cout << line << endl;
return 0;
}
/*
*用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
*string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容
cout << line << endl;
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有