2012年12月21日星期五

C++ Split String

Making some cross-platform stuffs and shifted back to C++ recently.
Trying to do something like NSString's componentsSeparatedByString function in C++.

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}


in the while loop, add those string into an array and return it back can do similiar job.

ref:cpluscplus.com

沒有留言:

發佈留言