c string字符串切割

 

static vector<string> splitEx(const string& src, string separate_character)  
{  
    vector<string> strs;  
      
    int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符  
    int lastPosition = 0,index = -1;  
    while (-1 != (index = src.find(separate_character,lastPosition)))  
    {  
        strs.push_back(src.substr(lastPosition,index - lastPosition));  
        lastPosition = index + separate_characterLen;  
    }  
    string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容  
    if (!lastString.empty())  
        strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队  
    return strs;  
}  
 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3     string s = "123,456,789,0,888";
 4     string del = ","; 
 5     vector<string> strs = splitEx(s, del); 
 6     for ( unsigned int i = 0; i < strs.size(); i++) 
 7     { 
 8         cout << strs[i].c_str() << endl;
 9     } 
10     return 0; 
11 }

 

posted @ 2015-08-17 18:34  KnightHwang  阅读(527)  评论(0编辑  收藏  举报