c++中string 的replace用法
来源:互联网 发布:怎么做淘宝客服 编辑:程序博客网 时间:2024/05/17 07:33
- /*用法一:
- *用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;
- }
- /*用法四: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;
- }
- /*用法五: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;
- }
运行结果:
- /*用法六: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;
- }
运行结果:
- /*用法七: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;
- }
运行结果:
- /*用法八:
- *用重复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;
- }
strcmp里如果用string类做参数的话,加上strcmp(b,c.c_str()),也就是c_str
0 0
- c++: string 的函数replace()用法
- c++中string 的replace用法
- c++中string 的replace用法
- C++string的replace函数
- Javascript 中 String.replace( ) 多种用法
- Javascript 中 String.replace( ) 多种用法
- JAVA中string.replace()和string.replaceAll()的区别及用法 数据库中[]转义
- 关于String的replace的用法注意事项
- c++ string 的函数replace()用法
- c++ string 的函数replace()用法
- C++中的string的replace用法
- c++ string 的函数replace()用法
- c++ string 的函数replace()用法
- Java String replace() 和 replaceAll()的用法
- c++ string 的函数replace()用法
- JAVA中string.replace()和string.replaceAll()的区别及用法
- JAVA中string.replace()和string.replaceAll()的区别及用法
- JAVA中string.replace()和string.replaceAll()的区别及用法
- Android开发优化之对界面UI的优化
- 如何在网页中嵌入自己想要的字体
- 一个C++字符串替换和截取的实例
- Mybatis查询之list作为参数查询
- Description Resource Path Location Type Java compiler level does not match the version of(图解)
- c++中string 的replace用法
- 移码的再总结
- 对Rxjava的理解以及结合Retrofit使用(一)
- Sublime Text无法使用Emmet插件(附带手动安装)
- Centos下Valgrind使用与安装
- qduoj 韬韬说做出这个泥萌就AK辣
- 分布式系统中生成全局唯一ID方案
- Windows Server 2008安装Python失败解决方案
- 线程安全性问题