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/