A vc ++ function similar to Java string [] Split (string RegEx)

Source: Internet
Author: User

Http://hi.baidu.com/ccskun/blog/item/9c4d033219ab5bfe1b4cff41.html/cmtid/1ce9b84445d57e2dcffca3d3

 

Int_ptr split_cstring (const cstring & source, // The original string to be truncated
Cstringarray & DEST, // split String Array
Const cstring & Division // delimiter string
) // Usage: Split (strviewstring, DEST, "<Div id =" pro_detail "> ");
{
If (source. isempty ())
Return-1;
DeST. removeall ();
Int Len = Division. getlength ();
Int Ifirst = 0;
Int ncount = 0;
Int Pos = 0;
Int pre_pos =-1;
While (-1! = POS)
{
If (-1 = pre_pos)
Pos = source. Find (Division, POS );
Else
Pos = source. Find (Division, (Pos + 1 ));

If (-1 = pre_pos)
{
Ifirst = 0;
If (-1 = POS)
Ncount = source. getlength ();
Else
Ncount = Pos;
}
Else
{
Ifirst = pre_pos + Len;
If (-1! = POS)
Ncount = pos-pre_pos-len;
Else
Ncount = source. getlength ()-pre_pos-len;
}

DeST. Add (source. mid (Ifirst, ncount ));

Pre_pos = Pos;
}

Return DeST. getcount ();

 

 

I wrote one,

Int pa_cstringsplit (cstring strsource, cstring strsplitter, cstringarray & sadestination ){
Int m_ilen_source = strsource. getlength ();
Int m_ilen_splitter = strsplitter. getlength ();
Int istart = 0;
Int ilen = 0;
Int IPOs = 0;
Sadestination. removeall ();
Do {
IPOs = strsource. Find (strsplitter, istart );
If (-1 = IPOS ){
Ilen = m_ilen_source-istart;
} Else {
Ilen = IPOs-istart;
}
Sadestination. Add (strsource. mid (istart, ilen ));
Istart + = ilen + m_ilen_splitter;
} While (istart <m_ilen_source );
Return true;
}

Related Article

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.