zoukankan      html  css  js  c++  java
  • 字符串的切割

    头文件:

     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));
    }

  • 相关阅读:
    HTML5拖拽
    HTML5地理定位
    HTML5文件读取
    HTML5全屏
    HTML5网络状态
    可爱的小黄人
    HTML5新增特性
    前端表单标签
    前端(表格)
    前端列表
  • 原文地址:https://www.cnblogs.com/haibianxiaolu/p/3881086.html
Copyright © 2011-2022 走看看