0

Possible Duplicate:
Splitting a string in C++

i need a suggestion on how to take one string of text and split it up based on a certain character, in this case "," without the use of any outside libraries

the lines of text are:

Amadeus,Drama,160 Mins.,1984,14.83
As Good As It Gets,Drama,139 Mins.,1998,11.3
Batman,Action,126 Mins.,1989,10.15
Billy Elliot,Drama,111 Mins.,2001,10.23
BR,SF,117,1982,11.98
Shadowlands,Drama,133 Mins.,1993,9.89
Shrek,Animation,93 Mins,2001,15.99
Snatch,Action,103 Mins,2001,20.67
The Lord of the Rings,Fantasy,178 Mins,2001,25.87
Community
  • 1
  • 1
Robert Spratlin
  • 305
  • 3
  • 5
  • 8
  • you can use Boost.Tokenizer, if that's an option for you – Andy Prowl Jan 13 '13 at 18:27
  • 1
    While, it does say whitespace in the question, there is lots in here about delimiters and standard-only: [Splitting a string in C++](http://stackoverflow.com/questions/236129/splitting-a-string-in-c) – chris Jan 13 '13 at 18:27
  • 1
    You know, this is actually a duplicate of one of the most viewed questions on SO. – Rapptz Jan 13 '13 at 18:28
  • I hope this link helps! http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c – AAA Jan 13 '13 at 18:29

2 Answers2

1

If you don't want to resort to other libraries (Boost.Tokenizer is a good choice IMO), here is some simple code for doing that:

#include <string>
#include <vector>

using namespace std;

vector<string> tokenize(string const& s, string const& separator)
{
    size_t start = 0;
    size_t pos = s.find(separator);

    vector<string> v;
    while (pos != string::npos)
    {
        string sub = s.substr(start, pos - start);
        v.push_back(sub);

        start = pos + 1;
        pos = s.find(separator, start);
    }

    string sub = s.substr(start, pos - start);
    v.push_back(sub);

    return v;
}

int main()
{
    string s = "asfa,adf,daf,c";
    vector<string> v = tokenize(s, ",");

    // Do what you want with v...

    return 0;
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • @RobertSpratlin: it compiles fine under GCC 4.7.2 and Clang 3.2. Are you compiling the whole example, or did you take out just the `tokenize()` function? if that's the case, did you include the `using namespace std` directive as well? also, did you `#include` the `` header? – Andy Prowl Jan 13 '13 at 19:46
  • @RobertSpratlin: btw you spelled "sepErator" instead of "sepArator", not sure if you have the same typo in your program – Andy Prowl Jan 13 '13 at 19:47
0

You could just find the indices of the commas and store them in a vector, then use string::substr (http://www.cplusplus.com/reference/string/string/substr/) to get the substrings between those indices.

jdesai927
  • 62
  • 1
  • 1
  • 5