浅谈C++中replace()方法
2015-11-10 13:29
791 查看
本文主要针对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; }
运行结果:
用法二:
/* *用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; }
运行结果:
注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。
您可能感兴趣的文章:
- php正则之函数 preg_replace()参数说明
- eregi_replace()中特殊字符的处理方法
- javascript replace()用法详解附实例代码
- js replace() 文本替换你所不知的
- javascript中字符串替换函数replace()方法与c# 、vb 替换有一点不同
- js中字符替换函数String.replace()使用技巧
- 从jQuery.camelCase()学习string.replace() 函数学习
- 关于JS字符串函数String.replace()
- PHP preg_replace() 正则替换所有符合条件的字符串
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- js正则查找match()与替换replace()用法实例
- 浅谈jQuery中replace()方法
- C++中replace()函数使用方法汇总
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息