C/C+:stringクラス関数使用

21386 ワード

  • str.erase()関数
  • str.size()、str.length()関数
  • str.find()関数
  • str.replace()関数
  • str.append()関数
  • str.substr()関数
  • str.assign()関数
  • str.insert()関数
  • str.erase()関数
    erase関数プロトタイプ(3つの使い方):(1)string& erase ( size_t pos = 0, size_t n = npos );(2)iterator erase ( iterator position );(3)iterator erase ( iterator first, iterator last );指定した位置の文字、文字列を削除します:erase(pos,n);posパラメータは、削除する文字の開始位置を表し、デフォルトは0です.nは削除する文字列の長さを表し、デフォルトはstring::npos;削除された文字列を返します.例えばerase(0,1)は最初の文字を削除します.
    erase(position); positionの文字を削除します(positionはstringタイプの反復器です)
    str.erase(str.begin()+9);
    

    erase(first,last); firstからlastまでの文字を削除します(firstとlastは反復器です).
    str.erase(str.bengin()+5,str.end()-8);
    

    str.size()、str.length()関数
    size()とlength()メンバー関数は、文字列内の文字数を返すことができます.string::nposは文字列に格納できる最大文字数です.
    str.find()関数
    関数プロトタイプ:(1)size_type find( const string& str, size_type pos=0)文字列のpos位置から、サブストリングstrを検索し、見つかれば、最初に出現した位置のインデックスを返し、見つからなければstring::npos(2)size_type find( const char* s, size_type pos=0)文字列のpos位置から、サブストリングsを検索し、見つかれば、最初に出現した位置のインデックスを返し、見つからなければ、戻りstring::npos(3)size_type find( const char* s, size_type pos=0,size_type n)文字列のpos位置からsの前のn文字からなるサブ文字列を検索し、見つけたらサブ文字列が初めて現れたときの先頭文字の位置のインデックスを返し、見つからなかったらstring::npos(4)size_type find(char ch , size_type pos=0)文字列pos位置から文字chを検索し、見つけたら、最初の出現位置のインデックスを返し、見つからない場合はstring::nposを返します.
    このほか、rfind()も定義されています.文字列または文字が最後に表示された場所find_を検索します.first_of():パラメータのいずれかの文字が文字列に最初に表示された場所find_を検索します.last_of() find_first_not_of() find_last_not_of()
    str.replace()関数
    機能:文字列内の要素またはサブ列を置換し、置換後の値を返します.関数プロトタイプ:(1)string& replace (size_t pos, size_t len, const string& str);元の文字列をstrで置き換えて開始位置posからlenの文字長である.使用例:
    #include
    #include
    using namespace std;
    int main()
    {
    	string str = "he is@ a@ good boy";
    	str=str.replace(str.find("a"),2,"#");  //    a            #
    	cout<<str<<endl; 
    	return 0;
    }
    

    (2)string& replace (const_iterator i1, const_iterator i2, const string& str);strで置き換え、反復器開始位置i 1から終了位置i 2までの範囲の文字.使用例:
    #include
    #include
    using namespace std;
    int main()
    {
    	string str = "he is@ a@ good boy";
    	 str=str.replace(str.begin(),str.begin()+5,"#"); // #   begin     5   
    	 cout<<str<<endl;
    	 return 0; 
    }
    

    (3)string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);サブストリングsubstrの所定の開始位置と長さで、元の文字列で指定された位置の文字列を置換する使用例:
    #include
    #include
    using namespace std;
    int main()
    {
    	string str = "he is@ a@ good boy";
    	string substr = "12345";
    	str=str.replace(0,5,substr,substr.find("1"),4); // substr        str     
    	cout << str << endl;
    	return 0; 
    }
    

    str.append()関数
    appendの使い方:文字列または文字の綴り関数の原型:(1)basic_string &append( const char *str );文字列の末尾にstr(2)basic_string &append( const basic_string &str, size_type index, size_type len );文字列の末尾にstrのサブ列を追加し、サブ列はindexインデックスで始まり、長さはlen(3)basic_string &append( const char *str, size_type num );文字列の末尾にstrのnum文字を追加(4)basic_string &append( size_type num, char ch );文字列の末尾にnum文字chを追加し、(5)basic_string &append( input_iterator start, input_iterator end );文字列の末尾に反復器startとendで表される文字列を追加
    使用法:
    string str1="I like C++";  
        string str2=",I like the world.";  
        string str3="Hello";  
        string str4("Hi");  
        //====================================  
        str1.append(str2);  
        str3.append(str2, 11, 7);  
        str4.append(5, '.');  
        //====================================  
        cout<<str1<<endl;  
        cout<<str3<<endl;  
        cout<<str4<<endl;  
        system("pause");  
        return 0;     
    }  
    

    実行結果:
    I like C++,I like the world.
    Hello World.
    Hi.....
    

    str.substr()関数
    substrの役割:サブ文字列をコピーし、指定した位置から指定した長さを設定します.関数プロトタイプ:string substr (size_t pos = 0, size_t len = npos) const;使用例:
    / string::substr
    #include 
    #include 
     
    int main ()
    {
      std::string str="We think in generalities, but we live in details.";
                                               // (quoting Alfred N. Whitehead)
      std::string str2 = str.substr (3,5);     // "think"
      std::size_t pos = str.find("live");      // position of "live" in str
      std::string str3 = str.substr (pos);     // get from "live" to the end
      std::cout << str2 << ' ' << str3 << '
    '
    ; return 0; }

    str.assign()関数
    assign作用:文字列に値を付ける(1)string& assign (const string& str);文字列strをオブジェクトに値を付ける使い方:
    string s1;
    string s2;
    s1.assign(s2);//   s1 = s2   
    

    (2)string& assign (const string& str, size_t subpos, size_t sublen);文字列strのサブストリングをオブジェクトに割り当て、サブストリングを開始位置subpos、長さsublenの文字列使用法:
     string str1("hello");
     string str2.assign(str1, 2, 3);
        :
     llo
    

    (3)string& assign (size_t n, char c);同じ文字をいくつか使って使う:
    string str5.assign(10, 'c');
      :
    cccccccccc
    

    (4)string& assign (const char* s, size_t n);1文字列の前のn文字の子列で値を割り当てる使い方:
    string str4.assign("World", 4);
      :
    Worl
    

    str.insert()関数
    Insert役割:文字列または文字関数プロトタイプ(1)basic_string& insert (size_type pos, const basic_string& str);元文字列の下にposと表記された文字の前に文字列str(2)basic_string& insert (size_type pos, const basic_string& str, size_type pos1, size_type n);元文字列の下にposと表記された文字の前にstrにposと表記された文字を挿入する前に、strにpos 1から始まるn文字を挿入する(3)basic_string& insert (size_type pos, size_type n, char c);元文字列の下にposと表記された文字の前にn文字cを挿入する
    使用例:
    #include
    using namespace std;
    int main()
    {
     string str="hello";
     string s="Hahah";
     str.insert(1,s);//      1   e      s
     cout<<str<<endl;
     
     string str1="hello";
     char c='w';
     str1.insert(4,5,c);//      4   o   5   c
     cout<<str1<<endl;
     
     string str2="hello";
     string s2="weakhaha";
     str2.insert(0,s2,1,3);//    s2    1 e   3   ,   eak,        0   h 
     cout<<str2<<endl;
     
     return 0;
    }
    
        :
    hHahahello
    hellwwwwwo
    eakhello