How to Split a CString object by delimeter in vc++? for example i have a string value "one+two+three+four" into a CString varable.
A:
Similar to this question:
CString str = _T("one+two+three+four");
int nTokenPos = 0;
CString strToken = str.Tokenize(_T("+"), nTokenPos);
while (!strToken.IsEmpty())
{
// do something with strToken
// ....
strToken = str.Tokenize(_T("+"), nTokenPos);
}
sje397
2010-06-30 07:42:33
Hi, Tokenize is not supported in VC6 MFC, But supported in ATL
Dharma
2010-06-30 09:07:04
You should probably add that requirement to the question.
sje397
2010-06-30 09:18:26
@mmonem Cheers. Fixed.
sje397
2010-06-30 09:34:56
A:
In VC6, where CString does not have a Tokenize method, you can defer to the strtok function and it's friends.
#include <tchar.h>
// ...
CString cstr = _T("one+two+three+four");
TCHAR * str = (LPCTSTR)cstr;
TCHAR * pch = _tcstok (str,_T("+"));
while (pch != NULL)
{
// do something with token in pch
//
pch = _tcstok (NULL, _T("+"));
}
// ...
sje397
2010-06-30 09:26:53
A:
CString sInput="one+two+three";
CString sToken=_T("");
int i = 0; // substring index to extract
while (AfxExtractSubString(sToken, sInput, i,','))
{
//..
//work with sToken
//..
i++;
}
Dharma
2010-06-30 09:27:11