c语言replace,浅谈C++中replace()方法

本文主要针对c++中常用replace函数用法给出九个样例程序:

用法一:

/*

*用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;

}

运行结果:

b194bc20409caa131612d36b522ca254.png

用法二:

/*

*用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;

}

运行结果:

00cc209b1e0575f190d84888d39dc3ae.png

用法三:

/*

*用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;

}

运行结果:

c157715ab729478bc8a6cf08099707c7.png

用法四:string转char*时编译器可能会报出警告,不建议这样做

/*

*用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;

}

运行结果:

8d89a8305ea7c39b843476d3a15cd0e9.png

用法五:string转char*时编译器可能会报出警告,不建议这样做

/*

*用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;

}

运行结果:

ec9aeda69ea45c0fd28fb113a78b27aa.png

用法六:string转char*时编译器可能会报出警告,不建议这样做

/*

*用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;

}

运行结果:

282f8e975f167b505376dc762a37975d.png

用法七:string转char*时编译器可能会报出警告,不建议这样做

/*

*用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;

}

运行结果:

cc0f000b75388f36511fcdd31fb8c6b4.png

用法八:

/*

*用重复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;

}

运行结果:

9d9345770fa1717deadfe0958819fd19.png

用法九:

/*

*用重复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;

}

运行结果:

7a1facc692a9acf50a3608e2bba20c20.png

注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值