MFC split string (go from CSDN)

Source: Internet
Author: User

A. _tcstok function

In MFC, a CString str= "AB AC dd PM", how to separate him into AB and AC and DD and PM these four strings, similar to the function of string split?

_tcstok Declaration Header file: <tchar. H>

char* _tcstok (char* strtoken, const char* strdelimit);

The function is able to intercept and return a token from a CString string, according to the provided delimiter;

Parameters :

Strtoken: is a string to be analyzed; This string contains one or more tokens, and of course separators, as well as

can have other characters;

Strdelimit: Is the delimiter, according to the delimiter to the strtoken in the analysis of tokens;

==============test1:_tcstok===============

STR is separated by a space-character delimiter

CString str = _t ("192.168.89.125");

TCHAR seps[] = _t (".");

tchar* token = _tcstok ((LPTSTR) (LPCTSTR) str, seps);

while (token! = NULL)

{

printf ("str=%s token=%s/n", Str,token);

token = _tcstok(NULL, SEPs);

}

==============test1:end==================

The results of the implementation are as follows:

str=192 token=192

str=192 token=168

str=192 token=89

str=192 token=125

At the first call, the function ignores the delimiter that appears at the beginning of the Strtoken string, returns the token pointer found, replaces the part (including the delimiter) with a null character (null character) and saves the "new" string into a static variable (system to complete) ;

If the first argument is NULL on the next call, the function takes the string from the static variable, gets and returns the new token based on the delimiter, replaces the found part (including the delimiter) with the null character (null character) and re-saves the "new" string; Until the loop condition ends.

Reference : Http://biao3730.spaces.live.com/Blog/cns!3415F543A07A7AE5!175.entry

You can also use char * __cdecl strtok(char *, const char *);(included in the header file <string.h>)

Reference:http://baike.baidu.com/view/1028553.htm

Two afxextractsubstring function

In addition, a composite string can be parsed using the afxextractsubstring function. Included in the header file <afxwin. The h>.

BOOL afxextractsubstring (cstring& rString, LPCTSTR lpszfullstring, int isubstring, TCHAR chsep/* =/)

Parameters :

RString: Used to store the substring you have taken out

lpszFullString: The entire string to split

Isubstring: The substring position you want to take, starting from 0

CHSEP: Specific Separator

========test2:afxextractsubstring========

CString str = _t ("192.168.89.125");

CString output = "";

for (int i=0; i<4; i++)

{

afxextractsubstring (output, str, I, '. ');

printf ("%s/n", output);

}

==============test2:end==================

The results of the implementation are as follows:

192

168

89

125

Reference : http://www.vckbase.com/bbs/prime/viewprime.asp?id=353

Three Custom Function String Split function

void Split (CString source, CString Divkey, cstringarray& dest)

{

Dest. RemoveAll ();

int pos = 0;

int pre_pos = 0;

while (-1! = pos) {

Pre_pos = pos;

pos = source. Find (Division, (pos+1));

Dest. ADD (source. Mid (Pre_pos, (Pos-pre_pos)));

}

}

Parameters:

Source: The string to be split

Divkey: Separator

Dest: Split result string array

MFC split string (go from CSDN)

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.