现在的位置: 首页 > 综合 > 正文

strtok 字符串分割函数

2013年10月20日 ⁄ 综合 ⁄ 共 613字 ⁄ 字号 评论关闭

这个函数好用的很, 可以使用某固定字符分割字符串。MSDN上的事例如下:

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 <string.h>
#include <stdio.h>

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

void main( void )
{
   printf( "%s/n/nTokens:/n", string );
   /* 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

A string   of ,,tokens
and some  more tokens

Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens

抱歉!评论已关闭.