分割字符串

 1 // 分割字符串
 2 vector<string> CSCSHbaseCSV::SplitStr(string strLine, char cDelim)
 3 {
 4     int nLen = strLine.length();
 5     int nStart = 0;
 6     int nEnd = 0;
 7     string strTemp;
 8     vector<string> vecStr;
 9     
10     while (nStart < nLen)
11     {
12         nEnd = strLine.find(delim, nStart);
13         if (nEnd == string::npos)
14         {
15             nEnd = nLen;
16         }
17         
18         strTemp = strLine.substr(nStart, nEnd - nStart);
19         trim_str(strTemp); // 消除前置、后置空白
20         vecStr.push_back(strTemp);
21         nStart = nEnd + 1;
22     }
23     return vecStr;
24 }
25 // 消除前置后置空白
26 void trim_str(string &str, const string &delem)
27 {
28     string::size_type epos;
29     string::size_type spos = str.find_first_not_of(delem);
30     if (string::npos == spos)
31     {
32         str.clear();
33         return ;
34     }
35     str.erase(0, spos);
36 
37     epos = str.find_last_not_of(delem);
38     str.erase(epos + 1, str.length() - epos - 1);
39 }

 参考:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html

posted @ 2014-11-25 08:43  冷冰若水  阅读(197)  评论(0编辑  收藏  举报