C++ 字符处理 strtok
Strtok 原形如下:
char *strtok(
char *strToken,
const char *strDelimit
);
// crt_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 <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

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

char *strtok(
char *strToken,
const char *strDelimit
);
Parameters
- strToken
- String containing token or tokens.
- strDelimit
- Set of delimiter characters.
Return Value
Returns 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
// crt_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 <string.h>
#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
int main( void )
{
printf( "Tokens:\n" );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}

Output
Tokens:
A
string
of
tokens
and
some
more
tokens
Notes:
Strtok(char *strToken, const char *strDelimit )
其中,strToken 和 strDelimit 一定要用字符数组格式的.也就是说.入口只能是字符数组元素地址


浙公网安备 33010602011771号