切割字串
將原本字串依特定符號分割為一個陣列
1: namespace StringPlus
2: {
3: /// Split a string by a char.
4: class SplitString
5: {
6: private:
7: std::vector<std::string> m_vec;
8:
9: public:
10: SplitString(const std::string& str, char symbol)
11: {
12: std::stringstream* _pss = new std::stringstream();
13: for(int i=0; i<str.size(); ++i)
14: {
15: if(str[i]==symbol)
16: {
17: m_vec.push_back(_pss->str());
18: delete _pss;
19: _pss = new std::stringstream();
20: }
21: else
22: {
23: (*_pss) << str[i];
24: }
25: }
26: m_vec.push_back(_pss->str());
27: delete _pss;
28: }
29:
30: int Size()
31: {return m_vec.size();}
32:
33: std::string& operator[](int i)
34: {return m_vec[i];}
35: };
36: }