微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/vb/262694.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐