Alex Rivera | Logout

Is There A Built-In Way to Split Strings In C++?

Asked 2009-03-01T15:50:04.457
20

well is there? by string i mean std::string

Edit
Report

1 Answer

1

The answer is no. You have to break them up using one of the library functions.

Something I use:

std::vector<std::string> parse(std::string l, char delim) 
{
    std::replace(l.begin(), l.end(), delim, ' ');
    std::istringstream stm(l);
    std::vector<std::string> tokens;
    for (;;) {
        std::string word;
        if (!(stm >> word)) break;
        tokens.push_back(word);
    }
    return tokens;
}

You can also take a look at the basic_streambuf<T>::underflow() method and write a filter.

answered 2009-03-01T15:55:04.367

Your Answer