標題:
substr() 函式
[打印本頁]
作者:
鄭繼威
時間:
2023-4-14 21:11
標題:
substr() 函式
試以 substr() 函式,
抓出字串
中特定範圍內的字串。
參數說明
substr(size_t pos, num);
size_t pos=你要抓的起始index
(記得index從0開始)
num=你要抓幾個
(預設全部)
需有<string> 標頭檔
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl; //56789
cout<<str.substr(2)<<endl; //23456789
cout<<str.substr(3,3)<<endl; //345
cout<<str.substr(7,1)<<endl; //7
system("pause");
return 0;
}
複製代碼
作者:
林雋喆
時間:
2023-4-15 15:07
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl; //56789
cout<<str.substr(2)<<endl; //23456789
cout<<str.substr(3,3)<<endl; //345
cout<<str.substr(7,1)<<endl; //7
system("pause");
return 0;
}
複製代碼
作者:
吳俊頡
時間:
2023-4-15 15:07
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl;
cout<<str.substr(2)<<endl;
cout<<str.substr(3,3)<<endl;
cout<<str.substr(7,1)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳宥霖
時間:
2023-4-15 15:07
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(9)<<endl;
cout<<str.substr(1)<<endl;
cout<<str.substr(6,3)<<endl;
cout<<str.substr(7,1)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳牧謙
時間:
2023-4-15 15:08
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(3,7)<<endl;
cout<<str.substr(7,3)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
翁川祐
時間:
2023-4-15 15:08
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="dfghyjukifh";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5,5)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
葉佳和
時間:
2023-4-15 15:08
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl;
cout<<str.substr(2)<<endl;
cout<<str.substr(3,3)<<endl;
cout<<str.substr(7,1)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳泓亦
時間:
2023-4-15 15:09
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="vckmbnnbgvcjn";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl;
system("pause");
return 0;
}
\
複製代碼
作者:
徐啟祐
時間:
2023-4-15 15:11
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main(){
string str="013276893721";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(4)<<endl;
cout<<str.substr(3,7)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
宜儒
時間:
2023-4-22 00:56
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string str="0123456789";
cout<<"原字串:"<<str<<endl;
cout<<str.substr(5)<<endl; //56789
cout<<str.substr(2)<<endl; //23456789
cout<<str.substr(3,3)<<endl; //345
cout<<str.substr(7,1)<<endl; //7
system("pause");
return 0;
}
複製代碼
作者:
羅暐傑
時間:
2023-4-22 11:07
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main() {
string str = "9876543210";
cout << "原字串:" << str << endl;
cout<<str.substr(5)<<endl;
cout<<str.substr(2)<<endl;
cout<<str.substr(3,3)<<endl;
cout<<str.substr(7,1)<<endl;
system("pause");
return 0;
}
複製代碼
作者:
楊芊琦
時間:
2023-5-6 17:59
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
string word = "abcdefg";
cout << word.substr() << endl;
cout << word.substr(0,3) << endl;
cout << word.substr(3,3) << endl;
cout << word.substr(1) << endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2