Bring your own strtok
paste a C-implemented function:
Invocation Example:
#define Max_cols
Char src[256];
Char *cols[max_cols];
int count;
int i;
strcpy (src, "AA aa BBC CCCD");
count = Splitex (src, ", cols, max_cols);
For (i=0; i<count; i++)
{
printf ("%d.%s\n", I+1, Cols[i]);
}
int Splitex (char *src, const char seperator, char **pcols, const int nmaxcols)
{
Char *p;
Char **pcurrent;
int count = 0;
if (nmaxcols <= 0)
{
return 0;
}
p = src;
pcurrent = pcols;
While (true)
{
*pcurrent = p;
pcurrent++;
count++;
if (Count >= nmaxcols)
{
Break ;
}
p = STRCHR (P, seperator);
if (p = = NULL)
{
Break ;
}
*p = ' + ';
p++;
}
return count;
}
c++///////////////////////////////////////////////////////////
#include <string>
#include <iostream>
#include <vector>
using namespace Std;
int main ()
{
String test = "AA aa BBC CCCD";
Vector<string> Strvec;
String strtemp;
String::size_type pos1, Pos2;
Pos2 = Test.find (");
POS1 = 0;
while (string::npos! = Pos2)
{
Strvec.push_back (Test.substr (POS1, pos2-pos1));
POS1 = Pos2 + 1;
Pos2 = Test.find (", POS1);
}
Strvec.push_back (Test.substr (POS1));
Vector<string>::iterator iter1 = Strvec.begin (), iter2 = Strvec.end ();
while (iter1! = iter2)
{
cout << *iter1 << Endl;
++iter1;
}
return 0;
}
Split, high efficiency