1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| int CCsvDlg::SplitString(const CString& input,
const CString& delimiter, CStringArray& results)
{
int iPos = 0;
int newPos = -1;
int sizeS2 = delimiter.GetLength();
int isize = input.GetLength();
CArray<INT, int> positions;
newPos = input.Find (delimiter, 0);
if( newPos < 0 ) { return 0; }
int numFound = 0;
while( newPos > iPos )
{
numFound++;
positions.Add(newPos);
iPos = newPos;
newPos = input.Find (delimiter, iPos+sizeS2+1);
}
for( int i=0; i <= positions.GetSize(); i++ )
{
CString s;
if( i == 0 )
s = input.Mid( i, positions[i] );
else
{
int offset = positions[i-1] + sizeS2;
if( offset < isize )
{
if( i == positions.GetSize() )
s = input.Mid(offset);
else if( i > 0 )
s = input.Mid( positions[i-1] + sizeS2,
positions[i] - positions[i-1] - sizeS2 );
}
}
if( s.GetLength() > 0 )
results.Add(s);
}
return numFound;
} |