返回列表 發帖

substr() 函式

試以 substr() 函式,抓出字串中特定範圍內的字串。

參數說明
substr(size_t pos,  num);
size_t pos=你要抓的起始index(記得index從0開始)
num=你要抓幾個(預設全部)

需有<string> 標頭檔
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  //56789
  10.     cout<<str.substr(2)<<endl;  //23456789
  11.     cout<<str.substr(3,3)<<endl;  //345
  12.     cout<<str.substr(7,1)<<endl;  //7
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  //56789
  10.     cout<<str.substr(2)<<endl;  //23456789
  11.     cout<<str.substr(3,3)<<endl;  //345
  12.     cout<<str.substr(7,1)<<endl;  //7
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  
  10.     cout<<str.substr(2)<<endl;  
  11.     cout<<str.substr(3,3)<<endl;  
  12.     cout<<str.substr(7,1)<<endl;  
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(9)<<endl;  
  10.     cout<<str.substr(1)<<endl;  
  11.     cout<<str.substr(6,3)<<endl;  
  12.     cout<<str.substr(7,1)<<endl;  
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(3,7)<<endl;  
  10.     cout<<str.substr(7,3)<<endl;  
  11.     system("pause");
  12.     return 0;   
  13. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="dfghyjukifh";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5,5)<<endl;
  10.    
  11.     system("pause");     
  12.     return 0;   
  13. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  
  10.     cout<<str.substr(2)<<endl;  
  11.     cout<<str.substr(3,3)<<endl;  
  12.     cout<<str.substr(7,1)<<endl;
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="vckmbnnbgvcjn";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  
  10.    
  11.     system("pause");     
  12.     return 0;   
  13. }

  14. \
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main(){
  6.     string str="013276893721";
  7.     cout<<"原字串:"<<str<<endl;
  8.     cout<<str.substr(4)<<endl;
  9.     cout<<str.substr(3,7)<<endl;
  10.     system("pause");
  11.     return 0;
  12. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<"原字串:"<<str<<endl;
  9.     cout<<str.substr(5)<<endl;  //56789
  10.     cout<<str.substr(2)<<endl;  //23456789
  11.     cout<<str.substr(3,3)<<endl;  //345
  12.     cout<<str.substr(7,1)<<endl;  //7
  13.     system("pause");     
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;

  5. int main() {
  6.     string str = "9876543210";
  7.     cout << "原字串:" << str << endl;
  8.     cout<<str.substr(5)<<endl;
  9.     cout<<str.substr(2)<<endl;  
  10.     cout<<str.substr(3,3)<<endl;  
  11.     cout<<str.substr(7,1)<<endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;

  5. int main()
  6. {
  7.     string word = "abcdefg";
  8.     cout << word.substr() << endl;
  9.     cout << word.substr(0,3) << endl;
  10.     cout << word.substr(3,3) << endl;
  11.     cout << word.substr(1) << endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

TOP

返回列表