首页 > 技术文章 > 拆分出含特定字符的字符串

Unclebigdata 2021-06-08 15:11 原文

  最近在处理个工具的开发,里面有涉及到读取出文件的所有的手机号码,然后读到的所有号码都存到了一个string类型中,每个号码都有" ,"来分隔,我这边需要把这个字符串的号码全部提出来 ,这样的话就涉及到字符串的简单抓 分了,实现如下: 

  std::vector<std::string> SplitMobile(const std::string &str,const std::string &pattern)
  {
    std::vector<std::string> resVec;
    if ("" == str)
    {
      return resVec;
    }

    std::string strs = str + pattern;
    int pos = strs.find(pattern);
    int size = strs.size();

    while (pos != std::string::npos)
    {
      std::string x = strs.substr(0,pos);
      resVec.push_back(x);
      strs = strs.substr(pos+1,size);
      pos = strs.find(pattern);
    }
    return resVec;
  }

  //这样的话就把string里面的号码 一个个提出来 了的,并存在容器vector当中。。

  pattern  //这个指字符串中的间隔的字符

推荐阅读