Split CString
Hi
I have a variable with the data type of CString
CString strCommand ;
strCommand = "SET 01";
I want to split this strCommand into
1. SET
2. 01
How can i do it and which should be more efficient as i need to repeat that step many times?, seems there is no split command for this.
# 1 Re: Split CString
using
1. find
2. left
3. right
method of cstring you can easily split string
# 2 Re: Split CString
I suggest looking at strtok() function that allows you to tokenize a text.
cilu at 2007-11-10 22:32:16 >

# 3 Re: Split CString
try this
CString strCommand(_T("Set 01"));
TCHAR str1[256], str2[256];
int Num;
memset(str1, 0, sizeof(TCHAR) * 256);
memset(str2, 0, sizeof(TCHAR) * 256);
sscanf(strCommand, _T("%s %s"), str1, str2);
sscanf(strCommand, _T("%s %d"), str1, &Num);
# 4 Re: Split CString
I want the result as array, lets say
CString str = SET 01
After spliting this, i would like to see as like below
arr[0] = "SET"
arr[1] = "01"
Is there any functions that will take CString as input and return array of CString.
Example :
Im managed C++ we can do as like below
String *strTemp = "SET 01";
__wchar_t split __gc[] = new __wchar_t __gc[1];
split [0] = ' ';
String __gc *strCmd __gc[] = strTemp-> Split (split);
I want to have same thing for CString in C++ core, more efficient.
# 5 Re: Split CString
Well, Managed C++ and C++ are not the same. System::String and MFC's CString don't have the same functionality. I repeat, use strtok.
cilu at 2007-11-10 22:35:18 >

# 6 Re: Split CString
As cilu already suggested for strtok
in CSting you will find same function example:
CString str( "First Second Third" );
CString resToken;
int curPos= 0;
resToken= str.Tokenize(_T(" "),curPos);
while (resToken != "")
{
resToken= str.Tokenize(_T(" "),curPos);
}
# 7 Re: Split CString
Here's how I did it:
int SplitString(const CString& strInputString, const CString& strDelimiter, CStringArray& arrStringArray)
{
int iPos = 0;
int newPos = -1;
int sizeS2 = strDelimiter.GetLength();
int isize = strInputString.GetLength();
CArray<INT, int> positions;
newPos = strInputString.Find (strDelimiter, 0);
if( newPos < 0 )
return 0;
int numFound = 0;
while( newPos > iPos )
{
numFound++;
positions.Add(newPos);
iPos = newPos;
newPos = strInputString.Find (strDelimiter, iPos + sizeS2);
}
for( int i=0; i <= positions.GetSize(); i++ )
{
CString s;
if( i == 0 )
s = strInputString.Mid( i, positions[i] );
else
{
int offset = positions[i-1] + sizeS2;
if( offset < isize )
{
if( i == positions.GetSize() )
s = strInputString.Mid(offset);
else if( i > 0 )
s = strInputString.Mid( positions[i-1] + sizeS2, positions[i] - positions[i-1] - sizeS2 );
}
}
arrStringArray.Add(s);
}
return numFound;
}
Calling it is really easy - just create a CStringArray, and pass it in something like this:
CStringArray cStringArray;
int nElementCount = SplitString("22|37|egg|234|17", "|", cStringArray);
In this example, nElementCount will be 5, using a delimiter of "|", and the cStringArray will have 5 elements:
22
37
egg
234
17
Hope that helps.
# 8 Re: Split CString
geeetee
please edit your post and use code tag so code become readable .
please go thru this link
http://www.dev-archive.com/forum/misc.php?do=bbcode#code
# 9 Re: Split CString
A classic example of "Reinventing the wheel"