返回列表 發帖

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. }
複製代碼

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6.     string st="dsanfj";
  7.     cout<<st.replace(2,1,"j")<<endl;
  8.    
  9.    
  10. system("pause");
  11. return 0;
  12. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string s="yomama";
  8.     cout<<"the string:"<<s<<endl;
  9.     cout<<s.replace(3,4,"other")<<endl;
  10.    
  11.     string s2="yomama";
  12.     cout<<"the string:"<<s2<<endl;
  13.     cout<<s2.replace(s2.find("ma"),4," ")<<endl;
  14.    
  15.     string s3="yomama";
  16.     cout<<"the string:"<<s3<<endl;
  17.     cout<<s3.replace(s3.begin(3),s3.end(),'a','a')<<endl;
  18.    
  19.     system("pause");
  20.     return 0;   
  21. }
複製代碼
Attention Seeker </3

TOP

  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. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;

  6. int main()
  7. {
  8.     string a1="bengchiling";
  9.     cout<<a1<<endl;
  10.     cout<<a1.replace(4,1,"Z")<<endl;
  11.     string a2="bengchiling";
  12.     cout<<a2.replace(a2.find("g"),1,"QQ")<<endl;
  13.     cout<<a2.replace(a2.find("g"),1,"QQ")<<endl;
  14.     string a3="bengchiling";
  15.     replace(a3.begin(),a3.end(),'n','o');
  16.     cout<<a3<<endl;
  17. }
複製代碼

TOP

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

TOP

返回列表