標題:
replace() 函式
[打印本頁]
作者:
鄭繼威
時間:
2023-8-18 16:56
標題:
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-8-18 21:02
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
string st="dsanfj";
cout<<st.replace(2,1,"j")<<endl;
system("pause");
return 0;
}
複製代碼
作者:
盧玄皓
時間:
2023-8-18 21:02
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="gfygosi";
cout<<str1.replace(2,1,"a")<<endl;
string str2="ho8yuptiu9e";
cout<<str2.replace(str2.find("8"),1,"k")<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張絜晰
時間:
2023-8-18 21:05
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string s="yomama";
cout<<"the string:"<<s<<endl;
cout<<s.replace(3,4,"other")<<endl;
string s2="yomama";
cout<<"the string:"<<s2<<endl;
cout<<s2.replace(s2.find("ma"),4," ")<<endl;
string s3="yomama";
cout<<"the string:"<<s3<<endl;
cout<<s3.replace(s3.begin(3),s3.end(),'a','a')<<endl;
system("pause");
return 0;
}
複製代碼
作者:
邱品惟
時間:
2023-8-22 20:18
#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-8-26 17:47
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str="hello";
cout<<"原:"<<str<<endl;
cout<<str.replace(1,2,"df")<<endl;
replace(str.begin(),str.end(),'l','a');
cout<<str;
system("pause");
return 0;
}
複製代碼
作者:
李宗儒
時間:
2024-1-16 20:12
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string a1="bengchiling";
cout<<a1<<endl;
cout<<a1.replace(4,1,"Z")<<endl;
string a2="bengchiling";
cout<<a2.replace(a2.find("g"),1,"QQ")<<endl;
cout<<a2.replace(a2.find("g"),1,"QQ")<<endl;
string a3="bengchiling";
replace(a3.begin(),a3.end(),'n','o');
cout<<a3<<endl;
}
複製代碼
作者:
朱奕祈
時間:
2024-4-16 19:57
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str1="apple0123456789";
cout<<str1.replace(4,7,"G")<<endl;
string str3="apple0123456789";
replace(str3.begin(),str3.end(),'p','g');
cout<<str3<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2