About the split function and the afxextractsubstring function (equivalent to the split function of VB)

Source: Internet
Author: User
In VC, the SPLIT function is constructed as follows:

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 ();
}

 

Or:

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;
}

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////

Split function under VC

Friends who are familiar with VB code should like the Split function very much, because it is too convenient to automatically Split the string as required. Microsoft hides a function in VC ++, the function is equivalent to the SPLIT function in VB. The function prototype is as follows:

BOOL AfxExtractSubString (CString & rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '/N ')

Parameter description:

The string obtained by rstring; the string to be split by lpszfullstring; the number of strings to be obtained by isubstring; the separator between chsep substrings. The default value is press Enter;

If the returned value is flase, The isubstring is out of bounds. Otherwise, the separation is successful.

For example, if there is a string strfullstring = "ABCD-hgdy-weiuiwu-sdlsk", then there are:

Cstring strtmp;

Afxextractsubstring (strtmp, (lpctstr) strfullstring, 0, '-'); // The content of strtmp is ABCD.

Afxextractsubstring (strtmp, (lpctstr) strfullstring, 2, '-'); // The content of strtmp is weiuiwu

It seems quite useful, but there are two restrictions:

1. functions that can only be used in MFC

2. Only characters can be used to separate strings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.