My First Generosity - split function

Here My first try to put some useful VC++ code..

This is function which similar to VB Split command more or less of STRTOK with output as an array.

If need include this as I am using CString for an array out.

//#include <afx.h>
//#include <afxcmn.h>

Function here follows.

CString *split(CString str,char delimiter)
{
long len=0;

if(str.IsEmpty())
return NULL;
len=str.GetLength();

CString *st;
st=new CString[len];
int c=0;
while(1)
{
int pos=str.Find(delimiter);
if(pos!=-1)
{
st[c]=str.Left(pos);
str.Delete(0,pos+1);
}
else
{
st[c]=str;
break;
}
c++;
}

return st;
}
This function can be used with different delimiters, as an example here below:

CString strFulltext = "Hello World this is Abhay Menon";
CString *strArry = split(strFulltext,' ');


output like:

strArray[0]="Hello"
strArray[1]="World"
strArray[2]="this"
strArray[3]="is"
strArray[4]="Abhay"
strArray[5]="Menon"


Well hope you like this piece of code..

Regards

Abhay M

Comments

Popular posts from this blog

Split function with std::List

Welcome to StackEditPro