共用方式為


basic_string::substr

複製最多字元的某個數字子字串從字串開頭的從指定的位置。

basic_string<CharType, Traits, Allocator> substr(
    size_type _Off = 0,
    size_type _Count = npos
) const;

參數

  • _Off
    設定項目中第一個複本所做的位置,其預設值為 0。

  • _Count
    要複製的字元數,如果存在的話)。

傳回值

運算元為字串開頭項目的複本在這個位置的子字串物件的第一個引數指定。

範例

// basic_string_substr.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;

   string  str1 ("Heterological paradoxes are persistent.");
   cout << "The original string str1 is: \n " << str1
        << endl << endl;

   basic_string <char> str2 = str1.substr ( 6 , 7 );
   cout << "The substring str1 copied is: " << str2
        << endl << endl;
   
   basic_string <char> str3 = str1.substr (  );
   cout << "The default substring str3 is: \n " << str3
        <<  "\n which is the entire original string." << endl;
}
  
  
  

需求

標題: <string>

命名空間: std

請參閱

參考

basic_string Class