Echo89

C++ explode

Nov 19th, 2012
54
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vector<string> explode(string str, string del)
  2. {
  3.     int slen = str.length();
  4.     int dlen = del.length();
  5.     int found_a;
  6.     vector<string> final;
  7.     vector<string> fail (0);
  8.     if(dlen == 0) return fail;
  9.     if(slen == 0) return fail;
  10.     while(str.find(del) != string::npos)
  11.     {
  12.         final.push_back(str.substr(0, str.find(del)));
  13.         str.erase(0, str.find(del) + dlen);
  14.     }
  15.  
  16.     if(str.length() > 0)
  17.     {
  18.         final.push_back(str);
  19.         str.erase(0, str.length());
  20.     }
  21.  
  22.     return final;
  23. }
RAW Paste Data