Split function written in MFC

Source: Internet
Author: User
Code

The four parameters are described as follows:
A: string to be split
B: The storage array, which must be defined to be sufficient to put down all the split results.
C: The maximum subscript of the actually generated array element after the split. If no element is generated, the value is-1 after the function is called. Note that the value of C must change after the function is called. This is an address reference parameter.
D: delimiter. The length is not limited to one character.
Void split (cstring A, cstring * B, Int & C, cstring D)
{
Int d_len = D. getlength ();
Int J = 0;
Int n = 0;
Int m_pos;
While (1)
{
M_pos = A. Find (D, J );
If (m_pos =-1 & J = 0)
{
C =-1;
Break;
}
 
If (m_pos =-1 & J! = 0)
{
B [N] = A. mid (J, A. getlength ()-j );
C = N;
Break;
}
 
If (j = 0)
{
B [N] = A. mid (0, m_pos );
J = m_pos + d_len;
}
Else
{
B [N] = A. mid (J, m_pos-j );
J = m_pos + d_len;
}
N ++;
}
}
The following is an example of the call method:
Cstring B [20];
Cstring A = "hello0 |! Nihao |! How are you |! 123456 |! 111 ";
Int C = 0;
Cstring d = "! ";
Cstring T;
 
Split (A, B, C, D );
If (C! =-1)
{
For (INT I = 0; I <= C; I ++)
{
Afxmessagebox (B [I]);
}
}
Else
{
Afxmessagebox ("this item cannot be found ");
}
 

 

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.