String2Array(sInputFormat,m_arrType,';');
int String2Array(const CString& s,CStringArray &sa,char chSplitter)
{
int nLen=s.GetLength(),nLastPos,nPos;
bool bContinue;
sa.RemoveAll();
nLastPos=0;
do
{
bContinue=false;
nPos = s.Find(chSplitter,nLastPos);
if (-1!=nPos)
{
sa.Add(s.Mid(nLastPos,nPos-nLastPos));
nLastPos=nPos+1;
if (nLastPos != nLen) bContinue=true;
}
} while (bContinue);
if (nLastPos != nLen)
sa.Add(s.Mid(nLastPos,nLen-nLastPos));
return sa.GetSize();
}
或者是:
int SplitString(CString & str,TCHAR cTok,CStringArray& aryItem)
{
TCHAR* p = str.GetBuffer(0);
TCHAR* e = p;
TCHAR cEnd = *e;
int nCount = 0;
while(cEnd)
{
if(*e == _T('/0'))
cEnd = *e;
else if(*e == cTok)
*e = _T('/0');
if(*e)
e++;
else
{
if(*p != _T('/0'))
{
aryItem.Add(p);
nCount++;
}
p = ++e;
}
}
return nCount;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VC下的Split函数
熟悉VB代码的朋友应该都极度喜欢Split函数,因为实在太方便了,自动帮你把字符串按照要求拆分,在VC++中微软隐藏了一个函数,其功效等同与VB中的SPLIT函数,该函数原型如下:
BOOL AfxExtractSubString(CString& rString,LPCTSTR lpszFullString,int iSubString,TCHAR chSep = '/n')
参数说明:
rString 得到的字符串;lpszFullString 待分割的字符串;iSubString 要得到第几个字符串;chSep 个子串之间的分隔符,默认是回车;
返回值为Flase表示iSubString 越界,否则分隔成功
例如,有一个字符串strFullString = "abcd-hgdy-weiuiwu-sdlsk";则有:
CString strTmp;
AfxExtractSubString( strTmp,(LPCTSTR)strFullString,'-');//strTmp的内容为abcd
AfxExtractSubString( strTmp,2,'-');//strTmp的内容为weiuiwu
感觉蛮好用的,但是有两个限制:
1.仅仅能在MFC下使用的函数
2.分隔只能使用字符,不能使用字符串。