C++ strings

Is there a good reference online that describes all the nice things you can do with C++ strings? Is there a way to do a “split” on a C++ string like you can with Perl?

SGI has the STL documented online here.

Strings are here.

I can’t find any specific “split” method, but it’s easy enough to write one:

#include <string>
#include <list>
#include <iostream>

using namespace std;
list<string>* split_string(string* str, string::value_type split_on) {
  list<string> *parts = new list<string>();

  string::size_type lpos = 0;
  while(1) {
    string::size_type pos = str->find(split_on,lpos);
    if(pos == str->npos) {
    } else {
      parts->push_back(str->substr(lpos, pos-lpos));
    lpos = pos + 1;
  return parts;
int main(int argc, char* argv[]) {

  string test_string("Oboe,Bassoon,English Horn,Contrabassoon");

  list<string> *parts = split_string(&test_string, ',');

  list<string>::iterator substrings = parts->begin();
  while(substrings != parts->end()) {
    cout << *substrings++ << endl;

Note: I am NOT a C++ expert, I’m just now starting to play with the language after a long break. The above may have subtle bugs of the sort that plague C++ (in particular, I’m unsure if the call to substr returns a heap or stack allocated “string” instance)…

Based on Metacom’s code, it looks like strtok is the function you want. The best reference for C++ strings is an experienced programmer.

strtok is hard to get right, unless C++ has cleaned it up considerably. It keeps an internal state as to where it is in the string, meaning you are forced to split strings one at a time. If you call strtok on one string and then call it on another, the old state is lost.

strtok appears to be part of the cruft C++ picked up from C and shouldn’t be used lightly: It doesn’t operate on members of the string class, it operates on the nul-terminated arrays of char C uses instead of a true string type. If you don’t care about the overhead using the C++ library involves, try the code here or here (search in page for ‘Explode’).

Becaues strtok is so lousy, as Derleth described, I’d use the Boost Tokenizer class.