Board logo

標題: replace() 函式 [打印本頁]

作者: 鄭繼威    時間: 2023-4-14 21:11     標題: replace() 函式

試以 replace() 函式,將字串中的目標對象以特定字串取代。
<string> 標頭檔與 <algorithm> 標頭檔皆有提供 replace() 函式,但其用法與效果略有不同。

string::replace 參數說明
replace (size_t pos,  size_t len,  string);
size_t pos=你要換的index
size_t len=你要換的長度
const string& str=你要換的字串


std::replace 參數說明
replace (ForwardIterator first, ForwardIterator last,
                old_value, new_value)
ForwardIterator first=開始
ForwardIterator last=結束
old_value=要換的字元
new_value=要換成的字元
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  //honolalu
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     //honolala
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 吳俊頡    時間: 2023-4-15 15:22

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  //honolalu
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 林雋喆    時間: 2023-4-15 15:22

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  //honolalu
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     //honolala
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 陳泓亦    時間: 2023-4-15 15:23

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  //honolalu
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 陳牧謙    時間: 2023-4-15 15:23

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,3,"a")<<endl;  
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 陳宥霖    時間: 2023-4-15 15:23

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(2,3,"c")<<endl;  
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),5,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 葉佳和    時間: 2023-4-15 15:24

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;   
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 翁川祐    時間: 2023-4-15 15:27

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str="dfghyjukifh";
  9.     cout<<"原字串:"<<str<<endl;
  10.     cout<<str.replace(2,2,"A")<<endl;
  11.     string str2="dfghyjukifh";
  12.     replace(str2.begin(),str2.end(),'d','e');
  13.     cout<<str2<<endl;   
  14.    
  15.     system("pause");     
  16.     return 0;   
  17. }
複製代碼

作者: 徐啟祐    時間: 2023-4-15 15:28

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main(){
  7.     string str1="efkiusef";
  8.     cout<<str1.replace(5,3,"a")<<endl;  
  9.    
  10.     string str2="efkiusef";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  
  12.    
  13.     string str3="efkiusef";
  14.     replace(str3.begin(),str3.end(),'u','a');
  15.     cout<<str3<<endl;
  16.    
  17.     system("pause");
  18.     return 0;
  19. }
複製代碼

作者: 宜儒    時間: 2023-4-22 00:59

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(5,1,"a")<<endl;  //honolalu
  10.    
  11.     string str2="honolulu";
  12.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu
  13.    
  14.     string str3="honolulu";
  15.     replace(str3.begin(),str3.end(),'u','a');
  16.     cout<<str3<<endl;     //honolala
  17.    
  18.     system("pause");
  19.     return 0;
  20. }
複製代碼

作者: 羅暐傑    時間: 2023-4-22 11:12

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main(){
  7.     string str1="qwertyuiop";
  8.     cout<<str1.replace(5,3,"abc")<<endl;  
  9.    
  10.     string str2="qwertyuiop";
  11.     cout<<str2.replace(str2.find("u"),1,"p")<<endl;  
  12.    
  13.     string str3="qwertyuiop";
  14.     replace(str3.begin(),str3.end(),'q','z');
  15.     cout<<str3<<endl;
  16.    
  17.     system("pause");
  18.     return 0;
  19. }
複製代碼

作者: 楊芊琦    時間: 2023-5-6 18:18

  1. #include<algorithm>
  2. using namespace std;

  3. int main()
  4. {
  5.     string word = "hohohohu";
  6.     cout << word.replace(0, 8, "Hello!") << endl;
  7.     cout << word << endl;
  8.     cout << word.replace(word.find("e"), 4, "ELLO") << endl;

  9.     string word2 = "hohohuhu";
  10.     replace(word2.begin(), word2.end(),'h','l');
  11.     cout << word2 << endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2