1. 程式人生 > >C++ replace() 函式用法詳解

C++ replace() 函式用法詳解

文主要針對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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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 = "[email protected] [email protected]
     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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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 = "[email protected] [email protected] 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. }  
執行結果:

:所有使用迭代器型別的引數不限於string型別,可以為vector、list等其他型別迭代器。