標題:
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=要換成的字元
#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;
}
複製代碼
作者:
吳俊頡
時間:
2023-4-15 15: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;
system("pause");
return 0;
}
複製代碼
作者:
林雋喆
時間:
2023-4-15 15: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;
}
複製代碼
作者:
陳泓亦
時間:
2023-4-15 15:23
#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;
system("pause");
return 0;
}
複製代碼
作者:
陳牧謙
時間:
2023-4-15 15:23
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(5,3,"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;
}
複製代碼
作者:
陳宥霖
時間:
2023-4-15 15:23
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="honolulu";
cout<<str1.replace(2,3,"c")<<endl;
string str2="honolulu";
cout<<str2.replace(str2.find("u"),5,"a")<<endl; //honolalu
string str3="honolulu";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
葉佳和
時間:
2023-4-15 15: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;
}
複製代碼
作者:
翁川祐
時間:
2023-4-15 15:27
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str="dfghyjukifh";
cout<<"原字串:"<<str<<endl;
cout<<str.replace(2,2,"A")<<endl;
string str2="dfghyjukifh";
replace(str2.begin(),str2.end(),'d','e');
cout<<str2<<endl;
system("pause");
return 0;
}
複製代碼
作者:
徐啟祐
時間:
2023-4-15 15:28
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string str1="efkiusef";
cout<<str1.replace(5,3,"a")<<endl;
string str2="efkiusef";
cout<<str2.replace(str2.find("u"),1,"a")<<endl;
string str3="efkiusef";
replace(str3.begin(),str3.end(),'u','a');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
宜儒
時間:
2023-4-22 00:59
#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;
}
複製代碼
作者:
羅暐傑
時間:
2023-4-22 11:12
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string str1="qwertyuiop";
cout<<str1.replace(5,3,"abc")<<endl;
string str2="qwertyuiop";
cout<<str2.replace(str2.find("u"),1,"p")<<endl;
string str3="qwertyuiop";
replace(str3.begin(),str3.end(),'q','z');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
作者:
楊芊琦
時間:
2023-5-6 18:18
#include<algorithm>
using namespace std;
int main()
{
string word = "hohohohu";
cout << word.replace(0, 8, "Hello!") << endl;
cout << word << endl;
cout << word.replace(word.find("e"), 4, "ELLO") << endl;
string word2 = "hohohuhu";
replace(word2.begin(), word2.end(),'h','l');
cout << word2 << endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2