string split


#include <string>
#include <vector>
using std :: string; // use the string object
using std::vector;  //使用vector
 
 
 
void Split(const std::string& src, const std::string& separator, std::vector<std::string>& dest);//函数原型
 
 
void Split (const std :: string & src, const std :: string & separator, std :: vector <std :: string> & dest) // divided into an array of strings
{
 
        // Parameter 1: string to be split; Parameter 2: as a character separator; 3 parameters: the string stored in the divided vector Vector
 
 string str = src;
 string substring;
 string::size_type start = 0, index;
 dest.clear();
 index = str.find_first_of(separator,start);
 do
 {
  if (index != string::npos)
  {   
   substring = str.substr(start,index-start );
   dest.push_back(substring);
   start =index+separator.size();
   index = str.find(separator,start);
   if (start == string::npos) break;
  }
 }while(index != string::npos);
 
 //the last part
 substring = str.substr(start);
 dest.push_back(substring);
}

Guess you like

Origin www.cnblogs.com/toujizhe/p/12079061.html