Function, to split a string
PHP Code:
#include <windows.h>
#include <vector>
void Split(const char* pszStrIn, const char delimiter, std::vector<char*>* vecStrOut)
{
int nStrInLen;
int nFoundPos;
char* pszTmp;
int nTmpLen;
nStrInLen = strlen(pszStrIn);
nFoundPos = 0;
for (int i=0; i<nStrInLen; )
{
nFoundPos = 0;
// ----------------------------------------
// Find Delimiter
// ----------------------------------------
for (int ii=i; ii<nStrInLen; ii++)
{
if ( (pszStrIn[ii] == delimiter) )
{
nFoundPos = ii;
break;
}
}
if (nFoundPos < 1)
nFoundPos = nStrInLen;
// ----------------------------------------
// push vector
// ----------------------------------------
nTmpLen = (nFoundPos-i)+1;
pszTmp = new char[nTmpLen];
memset(pszTmp, 0, nTmpLen);
memcpy(pszTmp, (char*)(pszStrIn+i), nTmpLen-1);
vecStrOut->push_back(pszTmp);
i += ((nFoundPos-i) + 1);
}
}
void FreeSplit(std::vector<char*>* vecStr)
{
for (std::vector<char*>::iterator it = vecStr->begin();
it!=vecStr->end(); ++it) {
delete *it;
}
}
Usage.
PHP Code:
std::vector<char*> str;
Split("Hello, how are you ?", ' ', &str);
for (u_int i=0; i<str.size(); i++)
printf("%s\n", str[i]);
FreeSplit(&str);
Result.
PHP Code:
Hello,
how
are
you
?
"It's So Easy"