1

I have a string "stack+ovrflow*newyork;" i have to split this stack,overflow,newyork

any idea??

Cute
  • 13,643
  • 36
  • 96
  • 112

6 Answers6

11

First and foremost if available, I would always use boost::tokenizer for this kind of task (see and upvote the great answers below)

Without access to boost, you have a couple of options:

You can use C++ std::strings and parse them using a stringstream and getline (safest way)

std::string str = "stack+overflow*newyork;";
std::istringstream stream(str);
std::string tok1;
std::string tok2;
std::string tok3;

std::getline(stream, tok1, '+');
std::getline(stream, tok2, '*');
std::getline(stream, tok3, ';');

std::cout << tok1 << "," << tok2 << "," << tok3 << std::endl

Or you can use one of the strtok family of functions (see Naveen's answer for the unicode agnostic version; see xtofls comments below for warnings about thread safety), if you are comfortable with char pointers

char str[30]; 
strncpy(str, "stack+overflow*newyork;", 30);

// point to the delimeters
char* result1 = strtok(str, "+");
char* result2 = strtok(str, "*");
char* result3 = strtok(str, ";");

// replace these with commas
if (result1 != NULL)
{
   *result1 = ',';
}
if (result2 != NULL)
{
   *result2 = ',';
}

// output the result
printf(str);
Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • when using strtok, beware of it's single-theadedness! – xtofl Jun 25 '09 at 14:40
  • @xtofl beware of any part of the STL and its single threadedness – Doug T. Feb 25 '11 at 22:56
  • @Doug T: if I'm correct, `strtok` is using the same global memory for any thread, whereas you can _choose_ to use e.g. different `string` objects in different threads. And if you don't, you're screwed too. – xtofl Feb 26 '11 at 08:03
  • @xtofl I'm not quite sure I understand what you're saying. None of the standard library of C or C++ is thread safe... Neither language has any concept of threading built into the language itself. But maybe I'm not following you exactly. – Doug T. Mar 01 '11 at 14:15
  • 1
    @Doug T: `strtok` is stateful, while `getline` uses the argument object's state. I imagined above code were wrapped into two functions. The function with `stringstream` uses local variables and thus is thread safe. The `strtok` version, even though it _appears_ to use thread-local variables, will cause race conditions since `strtok` is stateful. (cfr http://msdn.microsoft.com/en-us/library/2c8d19sb(v=vs.71).aspx) – xtofl Mar 01 '11 at 15:03
6

Boost tokenizer

Simple like this:

#include <boost/tokenizer.hpp>
#include <vector>
#include <string>
std::string stringToTokenize= "stack+ovrflow*newyork;";
boost::char_separator<char> sep("+*;");
boost::tokenizer< boost::char_separator<char> > tok(stringToTokenize, sep);
std::vector<std::string> vectorWithTokenizedStrings;
vectorWithTokenizedStrings.assign(tok.begin(), tok.end());

Now vectorWithTokenizedStrings has the tokens you are looking for. Notice the boost::char_separator variable. It holds the separators between the tokens.

Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42
4

See boost tokenizer here.

Cătălin Pitiș
  • 14,123
  • 2
  • 39
  • 62
2

There is another way to split a string using c/c++ :

First define a function to split a string:

//pointers of the substrings, assume the number of fields will not be over 5
char *fields[5];   
//str: the string to splitted
//splitter: the split charactor
//return the real number of fields or 0 if any error exits
int split(char* str, char *splitter)
{
    if(NULL == str) 
    {
        return 0;
    }

    int cnt;
    fields[0] = str;
    for(cnt = 1; (fields[cnt] = strstr(fields[cnt - 1], splitter)) != NULL && 
            cnt < 5; cnt++)
    {
        *fields[cnt] = '\0';
        ++fields[cnt];
    }
    return cnt;
}

then you can use this function to split string as following:

char* str = "stack+ovrflow*newyork;"
split(str, "+");
printf("%s\n", fields[0]); //print "stack"
split(fields[1], "*");
printf("%s\n", fields[0]); //print "ovrflow"
split(fields[1], ";");
printf("%s\n", fields[0]); //print "newyork"

this way will be more efficient and reusable

luowen
  • 21
  • 1
2

You can use _tcstok to tokenize the string based on a delimiter.

Naveen
  • 74,600
  • 47
  • 176
  • 233
2

This site has a string tokenising function that takes a string of characters to use as delimiters and returns a vector of strings.

Simple STL String Tokenizer Function

Salgar
  • 7,687
  • 1
  • 25
  • 39