試以 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;
- }
複製代碼 |