Board logo

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

作者: tonyh    時間: 2020-6-5 20:01     標題: replace() 函式

試以 replace() 函式,將字串中的目標對象以特定字串取代。
<string> 標頭檔與 <algorithm> 標頭檔皆有提供 replace() 函式,但其用法與效果略有不同。
  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. }
複製代碼

作者: 孫嘉駿    時間: 2020-6-5 20:20

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

作者: 李宇澤    時間: 2020-6-5 20: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. }
複製代碼

作者: 林政瑜    時間: 2020-6-5 20: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. }
複製代碼

作者: 董宸佑    時間: 2020-6-5 20:26

  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.     system("pause");  
  18.     return 0;
  19. }
複製代碼

作者: 黃宥華    時間: 2020-6-5 20:27

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

作者: 蔡忻霓    時間: 2020-6-5 20:30

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

作者: 陳宥穎    時間: 2020-6-5 20:31

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

作者: 黃辰昊    時間: 2020-7-3 18:22

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<sstream>
  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.     system("pause");
  18.     return 0;
  19. }
複製代碼





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