C++字符串操作


From: http://wenku.baidu.com/view/8212e6faf705cc17552709cd.html

/*
 * main.cpp
 *
 *  Created on: Aug 20, 2013
 *      Author: ZhangShaojian
 */

#include <iostream>
#include <string>

using namespace std;

int main() {
    string sz1("TEST1");
    string sz2 = "TEST2";
    char sz3[] = "ABCDEFGHIJKLMN";
    cout << sz1 << " & " << sz2 << " & " << sz3 << endl;

    string sz4(sz3);
    cout << sz4 << endl;

    /*指定從chs的索引3開始,最后復制4個字節*/
    string sz5(sz3, 3, 4);
    cout << sz5 << endl;

    string sz6("0123456789");
    string sz7(sz6);
    cout << sz7 << endl;

    string sz8("0123456789", 2, 3);
    string sz9(sz8);
    cout << sz9 << endl;

    /*將chs前3個字符作為初值構造*/
    string sz10(sz3, 3);
    cout << sz10 << endl;

    /*分配10個字符,初值都是'k'*/
    string sz11(10, 'K');
    cout << sz11 << endl;

    string sz12(10, 'K');
    cout << sz12 << endl;

    sz12 = "TEST";
    cout << sz12 << endl;

    sz12.assign("abc");
    cout << sz12 << endl;

    /*重新分配指定字符串的前3的元素內*/
    sz12.assign("AABBCC", 3);
    cout << sz12 << endl;

    /*Swap交換方法*/
    string sz13 = "AAA";
    string sz14 = "BBB";
    cout << sz13 << " is sz13." << endl;
    cout << sz14 << " is sz14." << endl;
    sz13.swap(sz14);
    cout << sz13 << " is sz13." << endl;
    cout << sz14 << " is sz14." << endl;

    string sz15 = "AAAA";
    sz15 += "BBBB";
    cout << sz15 << endl;
    /*方法可以添加字符串*/
    sz15.append("CCCC");
    cout << sz15 << endl;
    /*方法只能添加一個字符*/
    sz15.push_back('D');
    cout << sz15 << endl;

    string sz16 = "ABCDEFG";
    /*頭部插入*/
    sz16.insert(0, "");
    /*尾部插入*/
    sz16.insert(sz16.size(), "");
    /*中間插入*/
    sz16.insert(sz16.size() / 2, "");
    cout << sz16 << endl;

    string sz17 = "ABCDEFGH";
    /*從索引1到索引3    即刪除掉了BCD*/
    sz17.erase(1, 3);
    /*即將指定范圍內的字符替換成"",即變相刪除FG*/
    sz17.replace(2, 2, "");
    cout << sz17 << endl;

    string sz18 = "ABCDEFGH";
    cout << sz18.length() << endl;
    sz18.clear();
    cout << sz18.length() << endl;
    /*使用earse方法變相全刪除*/
    sz18 = "0123456789";
    cout << sz18.length() << endl;
    sz18.erase(0, sz18.length());
    cout << sz18.length() << endl;

    string sz19 = "ABCDEFGH";
    /*從索引2開始3個字節的字符全替換成"!!!!!"*/
    sz19.replace(2, 3, "!!!!!!");
    cout << sz19 << endl;

    string sz20 = "ABCDEFGH";
    string sz21 = "ABCDEFGH";
    /*比較字符串*/
    if (sz20 == sz21)
        cout << "sz20 == sz21" << endl;
    else
        cout << "sz20 != sz21" << endl;
    if (sz20 != sz21)
        cout << "sz20 != sz21" << endl;
    else
        cout << "sz20 == sz21" << endl;
    if (sz20 > sz21)
        cout << "sz20 > sz21" << endl;
    else
        cout << "sz20 <= sz21" << endl;
    if (sz20 <= sz21)
        cout << "sz20 <= sz21" << endl;
    else
        cout << "sz20 > sz21" << endl;

    string sz22 = "ABC";
    /*返回字符數量*/
    cout << sz22.size() << endl;
    cout << sz22.length() << endl;

    string sz23 = "ABCDEFG";
    /*返回字符的可能最大個數*/
    cout << sz23.max_size() << endl;

    string sz24;
    /*判斷字符串是否為空*/
    if (sz24.empty())
        cout << "sz24為空。" << endl;
    else
        cout << "sz24不為空" << endl;
    sz24 = sz24 + "ABC";
    if (sz24.empty())
        cout << "sz24為空。" << endl;
    else
        cout << "sz24不為空" << endl;

    string sz25 = "ABC111";
    /*存取單一字符*/
    cout << "use []:" << endl;
    for (int i = 0; i < sz25.length(); i++) {
        cout << sz25[i] << endl;
    }
    cout << endl;
    cout << "use at():" << endl;
    for (int i = 0; i < sz25.length(); i++) {
        cout << sz25.at(i) << endl;
    }
    cout << endl;

    string sz26 = "ABCDEF1234";
    const char* sz27 = sz26.c_str();
    const char* sz28 = sz26.data();
    cout << "use at():" << endl;
    for (int i = 0; i < sz26.length(); i++) {
        cout << "c_str():" << sz27[i] << endl;
        cout << "data():" << sz28[i] << endl;
    }
    cout << "c_str():" << sz27 << endl;
    cout << "data():" << sz28 << endl;

    string sz29 = "ABCDEFG";
    /*返回某個子字符串*/
    /*從索引5開始2個字節*/
    string sz30 = sz29.substr(5, 2);
    cout << sz30 << endl;

    /*find查找函數
     s.find(args,pos)    在s中從pos位置開始查找args的第一次出現;
     s.rfind(args,pos)    在s中從pos位置開始查找args的最后一次出現;
     s.find_first_not_of(args,pos)    在s中從pos位置開始查找第一個不屬於args的字符;
     s.find_last_not_of(args,pos)    在s中從pos位置開始查找最后一個不屬於args的字符;
     find操作的參數:args
     c,pos在s中,從下標pos標記的位置開始,查找字符c,pos的默認值為0
     s2,pos在s中,從下標pos標記的位置開始,查找string對象s2,pos的默認值為0*/
    string sz31 = "ABCDEFGH123";
    string pattern = "GH";
    string::size_type pos;
    /*從索引0開始,查找符合字符串“GH”的頭索引*/
    pos = sz31.find(pattern, 0);
    cout << pos << endl;
    string sz32 = sz31.substr(pos, pattern.size());
    cout << sz32 << endl;

    /*提供類似STL的迭代器支持*/
    string sz33 = "ABCDEFGH123";
    for (string::iterator iter = sz33.begin(); iter != sz33.end(); iter++) {
        cout << *iter << endl;
    }
    cout << endl;
    
    /*一個C++字符串存在三種大小:
    a)現有的字符數,函數是size()和length(),他們等效。Empty()用來檢查字符串是否為空。
    b)max_size()這個大小是指當前C++字符串最多能包含的字符數,
    很可能和機器本身的限制或者字符串所在位置連續內存的大小有關系。
    我們一般情況下不用關心他,應該大小足夠我們用的。
    但是不夠用的話,會拋出length_error異常
    c)capacity()重新分配內存之前string所能包含的最大字符數。
    這里另一個需要指出的是reserve()函數,這個函數為string重新分配內存。
    重新分配的大小由其參數決定,默認參數為0,這時候會對string進行非強制性縮減*/

    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM