標題:
replace() 函式
[打印本頁]
作者:
tonyh
時間:
2021-7-17 20:00
標題:
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;
}
複製代碼
作者:
林鼎傑
時間:
2021-7-17 20:15
#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;
}
複製代碼
作者:
王宇崴
時間:
2021-7-17 20:15
#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;
}
複製代碼
作者:
黃子倢
時間:
2021-7-17 20:15
#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;
}
複製代碼
作者:
朱奕祐
時間:
2021-7-17 20:16
#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;
}
複製代碼
作者:
許洧熏
時間:
2021-7-17 20:19
#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;
}
複製代碼
作者:
余柏緯
時間:
2021-7-17 20:33
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(0,2,"de")<<endl;
string str2="sdfjieajihs";
cout<<str2.replace(str2.find("f"),1,"e")<<endl;
string str3="soejodofoeodfoo";
replace(str3.begin(),str3.end(),'o','b');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
呂尚霖
時間:
2021-7-17 20:33
#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;
}
複製代碼
作者:
俞成章
時間:
2021-7-17 20:34
#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;
}
複製代碼
作者:
王秉鈞
時間:
2021-7-17 20:42
#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','j');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2