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