標題:
replace() 函式
[打印本頁]
作者:
tonyh
時間:
2020-6-5 20:01
標題:
replace() 函式
試以 replace() 函式,將字串中的目標對象以特定字串取代。
<string> 標頭檔與 <algorithm> 標頭檔皆有提供 replace() 函式,但其用法與效果略有不同。
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl; //honolalu
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl; //honolalu
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl; //honolala
system("pause");
return 0;
}
複製代碼
作者:
孫嘉駿
時間:
2020-6-5 20:20
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string y="honolulu";
cout<<y.replace(5,1,"a")<<endl;
string x="honolulu";
cout<<x.replace(x.find("u"),1,"a")<<endl;
string z="honolulu";
replace(z.begin(),z.end(),'u','a');
cout<<z<<endl;
system("pause");
return 0;
}
複製代碼
作者:
李宇澤
時間:
2020-6-5 20:22
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl; //honolalu
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl; //honolalu
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl; //honolala
system("pause");
return 0;
}
複製代碼
作者:
林政瑜
時間:
2020-6-5 20:24
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl;
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl;
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
董宸佑
時間:
2020-6-5 20:26
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl;
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl;
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃宥華
時間:
2020-6-5 20:27
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="banana";
cout<<str1.replace(5,1,"e")<<endl;
string str2="banana";
cout<<str2.replace(str2.find("a"),1,"e")<<endl;
string str3="banana";
replace(str3.begin(),str3.end(),'a','e');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
蔡忻霓
時間:
2020-6-5 20:30
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="love";
cout<<str1.replace(5,1"a")<<endl;
string str2="love";
cout<<str2.replace(str.find("u"),1,"a")<<endl;
string str3="love";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳宥穎
時間:
2020-6-5 20:31
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string s="oraoraoraoraora";
cout<<s.replace(0,2,"do")<<endl;
cout<<s<<endl;
string a="asdfghjkul;";
cout<<a.replace(a.find("u"),1,"a")<<endl;
string str3="banananandna";
replace(str3.begin(),str3.end(),'a','e');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃辰昊
時間:
2020-7-3 18:22
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,1,"a")<<endl;
string str2="honolulu";
cout<<str2.replace(str2.find("u"),1,"a")<<endl;
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2