C/C++における文字列切り取りの関数

2200 ワード

strtok関数で、その関数は
char *strtok( char *strToken, const char *strDelimit );
 C++         ,  MFC CString       SpanExcluding SpanIncluding            。
      strtok。strtok,SpanExcluding SpanIncluding    MSDN          

  
strToken   String containing token(s) 

strDelimit
Set of delimiter characters 
戻り値の説明
All of these functions return a pointer to the next token found in
strToken
. They return
NULL
when no more tokens are found. Each call modifies
strToken
by substituting a
NULL
character for each delimiter that is encountered.
例は次のとおりです.
Example
/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include 
#include 

char string[] = "A string\tof ,,tokens
and some more tokens"; char seps[] = " ,\t
"; char *token; void main( void ) { printf( "%s

Tokens:
", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s
", token ); /* Get next token: */ token = strtok( NULL, seps ); } }

Output
A string   of ,,tokens
and some  more tokens

Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens