关于Split函数和AfxExtractSubString函数等同于VB的Split函数

VC中SPLIT函数的构造如下:

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.分隔只能使用字符,不能使用字符串。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...