• 文章
  • 使用 split() 方法處理字串
釋出
2012年9月6日 (最後更新:2012年9月6日)

使用 split() 方法處理字串

評分:3.4/5 (424 票)
*****

處理文字時的一個常見用法是將文字視為一系列列,
就像系統日誌的列一樣,從中提取一個或多個,然後以不同
的方式使用它們,同時丟棄其他部分。令人驚訝的是,C++ 字串
缺少一個可以自我拆分的方法。替代方法 (strtok()) 實際上不是
C++ 解決方案,因為它涉及到使用 C 陣列。另一個問題是它的
底層特性:使用者必須傳送一個指標來幫助
strtok()。

在這個示例中,我從
string 派生了一個 splitstring 類。如果你有一個 splitstring 並想將它作為字串使用,你
可以,因為它本身就是一個字串。但如果你需要拆分字串,你也可以
拆分它。執行 splitstring 的 split() 方法的輸出是一個字串向量。
這更類似於高階語言,其中字串具有
split() 方法。
可能的擴充套件包括基於正則表示式
或固定字串進行拆分的方法。


為了在更大的專案中使用,類宣告應該放在
一個單獨的檔案中,以便包含在此以及你的程式碼中(但我假設
你已經知道了)。MAIN 定義也應該被註釋掉。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Class splitstring which adds method split()

// define MAIN if this is a standalone program
#define MAIN 1

#include <string>
#include <vector>
#include <iostream>
using namespace std;


class splitstring : public string {
    vector<string> flds;
public:
    splitstring(char *s) : string(s) { };
    vector<string>& split(char delim, int rep=0);
};

// split: receives a char delimiter; returns a vector of strings
// By default ignores repeated delimiters, unless argument rep == 1.
vector<string>& splitstring::split(char delim, int rep) {
    if (!flds.empty()) flds.clear();  // empty vector if necessary
    string work = data();
    string buf = "";
    int i = 0;
    while (i < work.length()) {
        if (work[i] != delim)
            buf += work[i];
        else if (rep == 1) {
            flds.push_back(buf);
            buf = "";
        } else if (buf.length() > 0) {
            flds.push_back(buf);
            buf = "";
        }
        i++;
    }
    if (!buf.empty())
        flds.push_back(buf);
    return flds;
}

#ifdef MAIN
main()
{
    // we define a string
    splitstring s("Humpty Dumpty sat on a wall.   Humpty Dumpty had a great fall");
    cout << s << endl;

    // splits and displays the vector of strings
    vector<string> flds = s.split(' ');
    for (int k = 0; k < flds.size(); k++)
        cout << k << " => " << flds[k] << endl;

    // now taking account of repeated delimiters
    cout << endl << "with repeated delimiters:" << endl;
    vector<string> flds2 = s.split(' ', 1);
    for (int k = 0; k < flds2.size(); k++)
        cout << k << " => " << flds2[k] << endl;

}
#endif