返回列表 發帖

substr() 函式

試以 substr() 函式,抓出字串中特定範圍內的字串。
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  8.     cout<<str.substr(5)<<endl;  //56789
  9.     cout<<str.substr(2)<<endl;  //23456789
  10.     cout<<str.substr(3,3)<<endl;  //345
  11.     cout<<str.substr(7,1)<<endl;  //7
  12.     system("pause");     
  13.     return 0;   
  14. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str="0123456789";
  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<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. //#define int ll
  5. #define FOR(i,a,b) for(int i=a;i<b;i++)
  6. #define REP(i,n) FOR(i,0,n)
  7. #define REP1(i,n) FOR(i,1,(n)+1)
  8. #define RREP(i,n) for(int i=(n)-1;i>=0;i--)
  9. #define f first
  10. #define s second
  11. #define pb push_back
  12. #define ALL(x) x.begin(),x.end()
  13. #define SZ(x) (int)(x.size())
  14. #define SQ(x) (x)*(x)
  15. #define pii pair<int,int>
  16. #define Graph vector<vector<int>>
  17. #define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
  18. const ll inf=(1ll<<63)-1;
  19. const int maxn=1e5+5;
  20. const ll mod=1e9+7;
  21. string s="0123456789";
  22. signed main()
  23. {
  24.     IOS();
  25.     cout<<s<<"\n";
  26.     cout<<s.substr(2)<<"\n";
  27.     cout<<s.substr(2,3)<<"\n";
  28.     cout<<s.substr(5,3)<<"\n";
  29.     return 0;
  30. }
複製代碼
Allen

TOP

  1. #include<bits/stdc++.h>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6.     string str="0123456789";
  7.     cout<<str.substr(5)<<endl;
  8.     cout<<str.substr(2)<<endl;
  9.     cout<<str.substr(3,3)<<endl;
  10.     cout<<str.substr(7,1)<<endl;   
  11.     return 0;   
  12. }
複製代碼
Ivy

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     cin.tie(0);
  6.     cin.sync_with_stdio(0);
  7.     string str="01234567891011";


  8.     cout<<str.substr(5)<<endl;
  9.     cout<<str.substr(2)<<endl;
  10.     cout<<str.substr(3,2)<<endl;
  11.     cout<<str.substr(7,1)<<endl;

  12.     return 0;
  13. }
複製代碼
hahahahahahahaha

TOP

返回列表