Question: > 
> **Possible Duplicate:**  
> 
> [How to split a string?](https://stackoverflow.com/questions/236129/how-to-split-a-string) 
> 
> 
> 

Hi,

I have a string say "1,0,1", how can i get the substring separated by comma operator.

Answer: 
another way of doing this is by using `strtok`. This is a old c way but it still applies to the problem.

```
using <vector>
using <string>

char* token, line[512];
std::string tokenStr;
std::string lineStr = "0, 1, 2";
std::vector<std::string> commaSplit;
strcpy ( line, lineStr.c_str());

//Remove spaces and find the first instance of ','
token = strtok( line, " ," );
while(token != NULL)
{
    //Copy the token to a string
    tokenStr = token;

    //Add the token to the vector
    commaSplit.push_back(token);

    //Find next instance of the ,
    token = strtok(NULL, " ,");
}

```
Search google for an algorithm to **explode** or **tokenize** your string. It's trivial.

You can also check out the documentation and use available tools : <http://www.cplusplus.com/reference/string/string/>

A simple implementation could be :

```
void tokenize(const string & text, vector<string> & tokens, char delim)
{
  size_t length = text.size();
  string token = "";

  for(size_t i=0;i<length;i++)
  {
      if(text[i] != delim)
      {
        token += text[i];
      }
      else
      {
        if(token.size() > 0)
        {
          tokens.push_back(token);
        }
        token = "";
      }
  }
  tokens.push_back(token);
}

```