码迷,mamicode.com
首页 > 编程语言 > 详细

(转)C++ replace() 函数用法详解

时间:2015-09-17 21:21:31      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

本文主要针对c++中常用replace函数用法给出样例程序

 

  1. /*用法一: 
  2.  *用str替换指定字符串从起始位置pos开始长度为len的字符 
  3.  *string& replace (size_t pos, size_t len, const string& str); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空  
  9.     cout << line << endl;     
  10.     return 0;  
  11. }  

运行结果:
技术分享

 

 

  1. /*用法二: 
  2.  *用str替换 迭代器起始位置 和 结束位置 的字符 
  3.  *string& replace (const_iterator i1, const_iterator i2, const string& str); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     line = line.replace(line.begin(), line.begin()+6, "");  //用str替换从begin位置开始的6个字符  
  9.     cout << line << endl;     
  10.     return 0;  
  11. }  

运行结果:

 

技术分享

 

  1. /*用法三: 
  2.  *用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 
  3.  *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     string substr = "12345";  
  9.     line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

 

  1. /*用法四:string转char*时编译器可能会报出警告,不建议这样做 
  2.  *用str替换从指定位置0开始长度为5的字符串 
  3.  *string& replace(size_t pos, size_t len, const char* s); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char* str = "12345";  
  9.     line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

 

  1. /*用法五:string转char*时编译器可能会报出警告,不建议这样做 
  2.  *用str替换从指定迭代器位置的字符串 
  3.  *string& replace (const_iterator i1, const_iterator i2, const char* s); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char* str = "12345";  
  9.     line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

 

  1. /*用法六:string转char*时编译器可能会报出警告,不建议这样做 
  2.  *用s的前n个字符替换从开始位置pos长度为len的字符串 
  3.  *string& replace(size_t pos, size_t len, const char* s, size_t n); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char* str = "12345";  
  9.     line = line.replace(0, 9, str, 4);  //用str的前4个字符替换从0位置开始长度为9的字符串  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

 

  1. /*用法七:string转char*时编译器可能会报出警告,不建议这样做 
  2.  *用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 
  3.  *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char* str = "12345";  
  9.     line = line.replace(line.begin(), line.begin()+9, str, 4);  //用str的前4个字符替换指定迭代器位置的字符串  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

 

  1. /*用法八: 
  2.  *用重复n次的c字符替换从指定位置pos长度为len的内容 
  3.  *string& replace (size_t pos, size_t len, size_t n, char c); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char c = ‘1‘;  
  9.     line = line.replace(0, 9, 3, c);    //用重复3次的c字符替换从指定位置0长度为9的内容  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

 

运行结果:

技术分享

 

  1. /*用法九: 
  2.  *用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 
  3.  *string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 
  4.  */  
  5. int main()  
  6. {  
  7.     string line = "this@ is@ a test string!";  
  8.     char c = ‘1‘;  
  9.     line = line.replace(line.begin(), line.begin()+9, 3, c);    //用重复3次的c字符替换从指定迭代器位置的内容  
  10.     cout << line << endl;     
  11.     return 0;  
  12. }  

运行结果:

 

技术分享

(转)C++ replace() 函数用法详解

标签:

原文地址:http://www.cnblogs.com/lou424/p/4817554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!