字符串的切割

头文件:

 Cstring trim(char ch = ' ');
 std::vector<Cstring> split(const Cstring & strSep,bool needTrim = true);

cpp 文件:

Cstring Cstring::trim(char ch)
{
    TrimLeft(ch);
    TrimRight(ch);
    return *this;
}

std::vector<Cstring> Cstring::split(const Cstring & strSep,bool needTrim)
{
    std::vector<Cstring> strsRet;

    if (needTrim == true)
    {
       trim();
    }
    for(;;)
    {
        Cstring temp;
        int pos = (int)find(strSep);
        if(pos == (int)bam_string::npos)
        {
            temp = needTrim?this->trim():*this;

            if(!temp.empty())
            {
                strsRet.push_back(temp);
            }
            break;
        }
        else
        {
            temp = substr(0,pos);
            if(needTrim == true)
            {
                temp = temp.trim();
            }
            if(!temp.empty())
            {
                strsRet.push_back(temp);
            }

            *this = this->substr(strSep.size()+pos,this->size() - strSep.size()+pos);

        }
    }

    return strsRet;
}

trim 的实现的备注:

void Cstring::TrimLeft(const char* trim )
{
    erase(0,find_first_not_of(trim));
}

posted on 2014-07-31 16:12  leafs  阅读(187)  评论(0编辑  收藏  举报

导航