Visual C + 6.0 allows free split of text strings

Source: Internet
Author: User
During Program Creation, you often need to split the text by fixed flag. The cstring class provides some basic string operations, this article uses the basic functions of the cstring class to freely split text strings.

I. Design of text string splitting and Classification

The text splitting is encapsulated into an independent class for code reuse. The class design is as follows:

Splitstr. h

Class csplitstr
{
PRIVATE:
// Delimiter
Cstring m_ssplitflag;
// The mark of continuous splitting is treated as a mark
Bool m_bsequenceasone;
// Split text
Cstring m_sdata;
Public:
// Get the split text string
Void getsplitstrarray (cstringarray & array );
// Obtain the split text
Cstring getdata ();
// Set the split text
Void setdata (cstring sdata );
// Obtain the splitting parameter.
Bool getsequenceasone () {return m_bsequenceasone ;};
// Set the splitting parameter
Void setsequenceasone (bool bsequenceasone) {m_bsequenceasone = bsequenceasone ;};
// Obtain the splitting mark.
Cstring getsplitflag () {return m_ssplitflag ;};
// Set the splitting flag
Void setsplitflag (cstring ssplitflag) {m_ssplitflag = ssplitflag ;};

Csplitstr ();
Virtual ~ Csplitstr ();
};

Implementation file: splitstr. cpp

// Constructor
Csplitstr: csplitstr ()
{
Setdata ("");
Setsequenceasone (true );
Setsplitflag (",");
}

Csplitstr ::~ Csplitstr ()
{
}

// Set the text function
Void csplitstr: setdata (cstring sdata)
{
M_sdata = sdata;
M_sdata.trimleft ();
M_sdata.trimright ();
}

Cstring csplitstr: getdata ()
{
Return m_sdata;
}
// Split the operation function (simple and practical)
Void csplitstr: getsplitstrarray (cstringarray & array)
{
Cstring sdata = getdata ();
Cstring ssplitflag = getsplitflag ();
If (sdata. Right (1 )! = Ssplitflag) sdata + = ssplitflag;

Cstring stemp;
Int Pos =-1;
While (Pos = sdata. Find (ssplitflag, 0 ))! =-1)
{
Stemp = sdata. Left (POS );
If (! Getsequenceasone ())
{
Array. Add (stemp );
}
Else
{
If (! Stemp. isempty () & stemp! = "") // Continuous separators are considered as a single process
{
Array. Add (stemp );
}
}
Sdata = sdata. Right (sdata. getlength ()-pos-1 );
}
}

Ii. Use of text string splitting and Classification

# Include <afxtempl. h>
# Include "splitstr. H"

Csplitstr split;
Cstring m_stext = "Hello, you are welcome to use text cut classification, Author: birds on the river, QQ: 36201365 ";
Split. setsplitflag (",");
Split. setsequenceasone (true );
Split. setdata (m_stext );
Cstringarray array;
Split. getsplitstrarray (array );

The best output result is:

Array [0] = "hello ";
Array [1] = "Welcome to Text Segmentation Classification ";
Array [2] = "Author: birds on the river ";
Array [3] = "QQ: 36201365 ";

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.