分割字符串(strsplit) 二

来源:互联网 发布:java高并发互联网框架 编辑:程序博客网 时间:2024/04/29 11:38
1、分割字符串(strsplit)

函数声明:
vector<string> strsplit( const string str, const string delim );


参数:
    str为待分割的字符串
    delim为分隔符的集合,注意:对""1:2,34/12,5,3/:4,5,6",如果delim是"/:",则结果是"1:2,34" ,"12/5,3" , "4,5,6"

返回值:
    被分割后的子字符串向量
      和strsplit一的区别是find是查找并返回与delim完全一致的位置,

而find_first_of(delim)) 返回的是查找并返回第一个包含在delim内任意字符位置(对""1:2,34/12,5,3/:4,5,6",如果delim是"/:",则结果是"1“ , "2,34"  , "12"  , " 5,3" , "4,5,6"
定义:

vector<string> strsplit( const string str, const string delim )
{
    int cutAt;
   
    string lstr = str;
    vector<string> result;
 while( (cutAt = lstr.find(delim)) != lstr.npos )


    {
        if(cutAt > 0)
        {
            result.push_back(lstr.substr(0,cutAt));
        }
        cout<<"lstr"<<lstr<<endl;
        lstr = lstr.substr(cutAt+delim.length());

    }
   
    if(lstr.length() > 0)
    {
        result.push_back(lstr);
    }
 
 return result;
}