C++基础---string类的replace
2015-09-03 19:55
1431 查看
1. string类的replace
1.1 string:string& replace (size_t pos, size_t len, const string& str);
原型:string& replace (size_t pos, size_t len, const string& str);说明:删除源字符串从下标为pos处开始的len个字符,然后在pos处插入string型字符串str。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; string str1="example"; cout<<"string& replace (size_t pos, size_t len, const string& str);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(15, 6, str1); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace (size_t pos, size_t len, const string& str); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.2 string:string& replace (const_iterator i1, const_iterator i2, const string& str);
原型:string& replace (const_iterator i1, const_iterator i2, const string& str);说明:删除源字符串从迭代i1到i2的所有字符,然后在迭代i1处插入string型字符串str。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; string str2="example"; cout<<"string& replace (const_iterator i1, const_iterator i2, const string& str);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(str.begin()+15, str.begin()+21, str2); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace (const_iterator i1, const_iterator i2, const string& str); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.3 substring:string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos);
原型:string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos);说明:删除源字符串从下标为pos处开始的len个字符,然后在pos处插入string型字符串str的从下标为subpos开始的sublen个字符组成的子字符串。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; string str3="this is a example"; cout<<"string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(15, 6, str3, 10, 7); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.4 c-string:string& replace(size_t pos, size_t len, const char* s);
原型:string& replace(size_t pos, size_t len, const char* s);说明:删除源字符串从下标为pos处开始的len个字符,然后在pos处插入char型字符串s。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace(size_t pos, size_t len, const char* s);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(15, 6, "example"); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace(size_t pos, size_t len, const char* s); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.5 c-string:string& replace(const_iterator i1, const_iterator i2, const char* s);
原型:string& replace(const_iterator i1, const_iterator i2, const char* s);说明:删除源字符串从迭代i1到i2的所有字符,然后在迭代i1处插入char型字符串s。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace(const_iterator i1, const_iterator i2, const char* s); "<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(str.begin()+15, str.begin()+21, "example"); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace(const_iterator i1, const_iterator i2, const char* s); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.6 buffer:string& replace(size_t pos, size_t len, const char* s, size_t n);
原型:string& replace(size_t pos, size_t len, const char* s, size_t n);说明:删除源字符串从下标为pos处开始的len个字符,然后在pos处插入char型字符串s前n个字符组成的子字符串。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace(size_t pos, size_t len, const char* s, size_t n); "<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(15, 6, "example is example", 7); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace(size_t pos, size_t len, const char* s, size_t n); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.7 buffer:string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
原型:string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);说明:删除源字符串从迭代i1到i2的所有字符,然后在迭代i1处插入char型字符串s前n个字符组成的子字符串。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(str.begin()+15, str.begin()+21, "example is example", 7); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 源字符串 :this is a test string. 目标字符串 :this is a test example.
1.8 fill:string& replace(size_t pos, size_t len, size_t n, char c);
原型:string& replace(size_t pos, size_t len, size_t n, char c);说明:删除源字符串从下标为pos处开始的len个字符,然后在pos处插入n个字符c。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace(size_t pos, size_t len, size_t n, char c);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(14, 8, 1, '!'); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace(size_t pos, size_t len, size_t n, char c); 源字符串 :this is a test string. 目标字符串 :this is a test!
1.9 fill:string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
原型:string& replace (const_iterator i1, const_iterator i2, size_t n, char c);说明:删除源字符串从迭代i1到i2的所有字符,然后在迭代i1处插入n个字符c。
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; cout<<"string& replace (const_iterator i1, const_iterator i2, size_t n, char c);"<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(str.begin()+14, str.begin()+22, 1, '!'); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 源字符串 :this is a test string. 目标字符串 :this is a test!
1.10 range:template < class InputIterator>string& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
原型:template < class InputIterator>string& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);说明: Copies the sequence of characters in the range [first,last), in the same order.
代码示例:
#include <iostream> #include <string> using namespace std; int main () { string str="this is a test string."; string str4="this is a example"; cout<<"template < class InputIterator>string& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last); "<<endl; cout<<"源字符串 :"<<str<<endl; str.replace(str.begin()+15, str.begin()+21, str4.begin()+10, str4.begin()+17); cout<<"目标字符串:"<<str<<endl; system("pause"); return 0; } =>template < class InputIterator>string& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last); 源字符串 :this is a test string. 目标字符串 :this is a test example.
参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/string/
相关文章推荐
- 零基础学C语言 笔记十 指针
- 字符串C++整理
- C++入门经典 笔记 (第十一章)开发高级指针
- C++基础---string类的insert
- 条款05 了解c++默默编写并调用哪些函数。。。
- C++ pair 用法
- 零基础学C语言 笔记九 字符串
- C++中lambda表达式
- C++实现斐波那契数列/青蛙跳台阶
- c++ primer 学习之路 一
- C++ Primer 学习笔记_98_特殊工具与技术 --优化内存分配
- C++ Primer 学习笔记_99_特殊工具与技术 --优化内存分配[续1]
- Effective C++——条款17(第3章)
- C++基础---string类的clear/erase/pop_back
- C++基础---string类的find/find_first_of/find_first_not_of/rfind/find_last_of/find_last_not_of
- C++基础---string类的operator==/operator!=/operator</operator<=/operator>/operator>=/compare
- C++函数中那些不可以被声明为虚函数的函数
- C++基础---string类的operator=/assign
- C语言中链表怎么删除结点?
- C语言中链表怎么删除结点?