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