C++_浅谈C++中replace()方法,本文主要针对c++中常用replace函
/IT之家 IT之家用str替换指定字符串从起始位置pos开始长度为len的字符 IT之家string& replace (size_t pos, size_t len, const string& str); IT之家/ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空 cout << line << endl; return 0; }
运行结果:
用法二:
/IT之家 IT之家用str替换 迭代器起始位置 和 结束位置 的字符 IT之家string& replace (const_iterator i1, const_iterator i2, const string& str); IT之家/ 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; }
运行结果:
用法三:
/IT之家 IT之家用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 IT之家string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); IT之家/ 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; }
运行结果:
用法四:string转charIT之家时编译器可能会报出警告,不建议这样做
/IT之家 IT之家用str替换从指定位置0开始长度为5的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串 cout << line << endl; return 0; }
运行结果:
用法五:string转charIT之家时编译器可能会报出警告,不建议这样做
/IT之家 IT之家用str替换从指定迭代器位置的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串 cout << line << endl; return 0; }
运行结果:
用法六:string转charIT之家时编译器可能会报出警告,不建议这样做
/IT之家 IT之家用s的前n个字符替换从开始位置pos长度为len的字符串 IT之家string& replace(size_t pos, size_t len, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串 cout << line << endl; return 0; }
运行结果:
用法七:string转charIT之家时编译器可能会报出警告,不建议这样做
/IT之家 IT之家用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 IT之家string& replace (const_iterator i1, const_iterator i2, const charIT之家 s, size_t n); IT之家/ int main() { string line = "this@ is@ a test string!"; charIT之家 str = "12345"; line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串 cout << line << endl; return 0; }
运行结果:
用法八:
/IT之家 IT之家用重复n次的c字符替换从指定位置pos长度为len的内容 IT之家string& replace (size_t pos, size_t len, size_t n, char c); IT之家/ 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; }
运行结果:
用法九:
/IT之家 IT之家用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 IT之家string& replace (const_iterator i1, const_iterator i2, size_t n, char c); IT之家/ 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; }
运行结果:
注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/cjj/8822.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
只需要在调用Ctrl+B编译后
时间:2021-01-13
-
OpenGL超级宝典visual studio
时间:2021-01-04
-
Directx11 教程(2) 基本的wi
时间:2021-01-04
-
LeetCode11ContainerWithMostWate
时间:2021-01-04
-
C语言简单IT之家速成
时间:2020-12-27
-
三分钟了解Activity工作流
时间:2020-12-27
-
编译器是如何实现32位整型
时间:2020-12-27
-
C++中lower_bound函数和upper
时间:2020-12-27
热门文章
-
LeetCode11ContainerWithMostWater(最大水容器)
时间:2021-01-04
-
C语言简单编程速成
时间:2020-12-23
-
都2020了,这五个最佳C++的IDE你还没用过?
时间:2020-12-23
-
C语言源程序文件的后缀是什么?
时间:2020-12-23
-
OpenGL超级宝典visual studio 2013开发环境配置
时间:2021-01-04
-
编译器是如何实现32位整型的常量整数除
时间:2020-12-27
-
libusbwin32学习笔记(二)
时间:2020-12-27
-
C语言简单IT之家速成
时间:2020-12-27
-
C语言和Python语言有什么区别呢?
时间:2020-12-24
-
C++对象模型之RTTI的实现原理
时间:2020-12-23
