C语言函数strtok

STRTOK(3)                                               Linux Programmer's Manual                                              STRTOK(3)

NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *str, const char *delim);

       char *strtok_r(char *str, const char *delim, char **saveptr);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       strtok_r(): _SVID_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

DESCRIPTION
       The  strtok() function breaks a string into a sequence of zero or more nonempty tokens.  On the first call to strtok() the string to be parsed should be specified in str.  In each subsequent call that should parse the same string, str must be NULL.

        The delim argument specifies a set of bytes that delimit the tokens in the parsed  string.   The  caller  may  specify  different strings in delim in successive calls that parse the same string. 

        Each  call to strtok() returns a pointer to a null-terminated string containing the next token.  This string does not include the delimiting byte.  If no more tokens are found, strtok() returns NULL. 

        A sequence of calls to strtok() that operate on the same string maintains a pointer that determines the point from which to start searching  for the next token.  The first call to strtok() sets this pointer to point to the first byte of the string.  The start of the next token is determined by scanning forward for the next nondelimiter byte in str.  If such a byte is found, it is  taken as  the  start of the next token.  If no such byte is found, then there are no more tokens, and strtok() returns NULL.  (A string that is empty or that contains only delimiters will thus cause strtok() to return NULL on the first call.)

       The end of each token is found by scanning forward until either the next delimiter byte is found or until  the  terminating  null byte ('\0') is encountered.  If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and strtok() saves a pointer to the following byte; that pointer will be used as the starting  point  when  searching  for  the  next token.  In this case, strtok() returns a pointer to the start of the found token.

       From  the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter, and that delimiter bytes at the start or end of the string are  ignored.   Put  another  way:  the tokens  returned  by  strtok() are always nonempty strings.  Thus, for example, given the string "aaa;;bbb,", successive calls to strtok() that specify the delimiter string ";," would return the strings "aaa" and "bbb", and then a null pointer.

       The strtok_r() function is a reentrant version strtok().  The saveptr argument is a pointer to a char *  variable  that  is  used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

       On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored.  In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

       Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

RETURN VALUE
       The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌───────────┬───────────────┬───────────────────────┐
       │Interface  │ Attribute     │ Value                 │
       ├───────────┼───────────────┼───────────────────────┤
       │strtok()   │ Thread safety │ MT-Unsafe race:strtok │
       ├───────────┼───────────────┼───────────────────────┤
       │strtok_r() │ Thread safety │ MT-Safe               │
       └───────────┴───────────────┴───────────────────────┘
CONFORMING TO
       strtok()
              POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.

       strtok_r()
              POSIX.1-2001, POSIX.1-2008.
BUGS
       Be cautious when using these functions.  If you do use them, note that:

       * These functions modify their first argument.

       * These functions cannot be used on constant strings.

       * The identity of the delimiting byte is lost.

       * The strtok() function uses a static buffer while parsing, so it's not thread safe.  Use strtok_r() if this matters to you.

EXAMPLE
       The program below uses nested loops that employ strtok_r() to break a string into a two-level hierarchy  of  tokens.   The  first
       command-line argument specifies the string to be parsed.  The second argument specifies the delimiter byte(s) to be used to sepa‐
       rate that string into "major" tokens.  The third argument specifies the delimiter byte(s) to be  used  to  separate  the  "major"
       tokens into subtokens.

       An example of the output produced by this program is the following:

           $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
           1: a/bbb///cc
                    --> a
                    --> bbb
                    --> cc
           2: xxx
                    --> xxx
           3: yyy
                    --> yyy

   Program source

       #include <stdio.h>
       #include <stdlib.h>
       #include <string.h>

       int
       main(int argc, char *argv[])
       {
           char *str1, *str2, *token, *subtoken;
           char *saveptr1, *saveptr2;
           int j;

           if (argc != 4) {
               fprintf(stderr, "Usage: %s string delim subdelim\n",
                       argv[0]);
               exit(EXIT_FAILURE);
           }

           for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
               token = strtok_r(str1, argv[2], &saveptr1);
               if (token == NULL)
                   break;
               printf("%d: %s\n", j, token);

               for (str2 = token; ; str2 = NULL) {
                   subtoken = strtok_r(str2, argv[3], &saveptr2);
                   if (subtoken == NULL)
                       break;
                   printf(" --> %s\n", subtoken);
               }
           }

           exit(EXIT_SUCCESS);
       }

       Another example program using strtok() can be found in getaddrinfo_a(3).

SEE ALSO
       index(3), memchr(3), rindex(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strstr(3), wcstok(3)

COLOPHON
       This  page  is  part  of  release 4.04 of the Linux man-pages project.  A description of the project, information about reporting
       bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man-pages/.

GNU                                                            2015-08-08                                                      STRTOK(3)

那个说明翻译过来就是这个

函数将字符串分解为一个零个或多个非空令牌的序列。在第一次调用strtok ()时,应该在str中指定要分析的字符串。在每次应该解析同一个字符串的调用中,str必须为null。

delim参数指定一组字节,用于分隔已分析字符串中的标记。调用方可以在解析同一个字符串的连续调用中在delim中指定不同的字符串。

对strtok ()的每个调用返回一个指向包含下一个令牌的空终止字符串的指针。此字符串不包括定界字节。如果未找到更多的令牌, strtok ()将返回null。

在同一个字符串上操作的strtok()调用序列维护一个指针,它确定要从哪个点开始搜索下一个令牌。对strtok ()的第一个调用将此指针设置指向字符串的第一个字节。下一个令牌的开始是通过扫描str中的下一个nondelimiter字节确定的。如果找到了这样的字节,则将其作为下一个令牌的开始。如果未找到此类字节,则不会有更多的令牌, strtok()将返回null。(因此,空或仅包含分隔符的字符串将导致strtok()在第一个调用上返回null。

每个令牌的结尾通过扫描找到,直到找到下一个分隔符字节或遇到终止的空字节( ' \ 0)为止。如果找到了分隔符字节,则用null字节覆盖它来终止当前令牌, strtok()将一个指针保存到以下字节;当搜索下一个令牌时,该指针将用作起始点。在这种情况下, strtok()返回一个指向已找到令牌的开始的指针。

从上面的描述中,它遵循已分析字符串中两个或多个连续分隔符的序列被视为一个分隔符,并且忽略字符串开头或结尾的分隔符字节。换句话说,由strtok()返回的令牌总是非空字符串。例如,给定字符串“AAA”;;BBB ,连续调用指定分隔符字符串的strtok ()。,”将返回字符串“AAA”和“BBB”,然后返回空指针。

strtok()函数是可重入版本strtok()。saveptr参数是指向字符()在内部使用的char*变量的指针,以便在解析同一字符串的连续调用之间保持上下文。

在对strtok()的第一个调用上,str应该指向要分析的字符串,并且忽略saveptr的值。在后续调用中, str应该为null ,saveptr应该自上次调用以来保持不变。

可以使用调用序列()序列同时解析不同的字符串,以指定不同的saveptr参数。

解释

从s开头开始的一个个被分割的串。当查找不到delim中的字符时,返回NULL。
所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

作用

strtok()用来将字符串分割成一个个片段。参数s指向欲分割的 字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的 指针。

strtok函数第一次调用时会把s字符串中所有在delim中出现的字符替换为NULL。然后通过依次调用strtok(NULL, delim)得到各部分子串。

// crt_strtok.c
// compile with: /W3
// 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>
#include <Windows.h>

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

int main( void )
{
    printf( "英文测试:\n" );

    // Establish string and get the first token:
    token = strtok( string, seps ); // C4996
    // Note: strtok is deprecated; consider using strtok_s instead
    while( token != NULL )
    {
        // While there are tokens in "string"
        //这里循环判断返回的token是否为NULL
        //如果不为NULL,继续分割
        printf( " %s\n", token );

        // Get next token: 
        token = strtok( NULL, seps ); // C4996
    }


    printf("原字符串: %s\n", string);   // 结果为 A, 说明原字符串已发生改变


    printf( "\n中文测试:\n");

    token = strtok( chinese, ",");
    while(token != NULL )
    {
        printf( "%s\n", token);

        token = strtok( NULL, ",");
    }

    printf("\n");

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值