(standard c libraries translation )strtok

strtok, strtok_r - extract tokens from strings
strtok,strtok_r-从字符串中提取标记

所需头文件:
#include <string.h>

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

The strtok() function parses a string into a sequence of 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 should be NULL.
strtok函数将字符串转换成一系列标记,第一次调用strtok的时候,需要解析的字符串必须明确制定,以后每次调用都会解释相同的字符串,所以str应该为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.
delim参数是一系列字节,把需要解析的字符串限定成标记,调用者在调用成功后会把字符串用delim解析成不同的标记

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.
每一次调用strtok都会返回一个指向截断字符串标记的指针,这个字符串不包含限定字节,如果没有找到标记,strtok返回NULL

A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter.  Delimiter bytes at the start or  end of the string are ignored.  Put another way: the tokens returned by strtok() are always nonempty strings.
需要解析的字符串中一系列两个或者更多的连续限定字节被当作单个限定符,处在字符串开头或者结尾的限定字节将会被忽略,顺便说一句,被strtok返回的标记一般是非空字符串


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.
strtok_r函数是strtok的可重入版本,saveptr参数是指向char *变量的指针,用来保存strtok_r成功调用之后的环境

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.
第一次调用strtok_r,str指针必须指向需要解析的字符串,saveptr的值被忽略掉,在后面的调用中,str必须是NULL,saveptr的值以后不能被改动了

Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.
在同一时刻有多个字符串在解析的话就必须使用strtok_r函数,这样子会把环境存放在不同的saveptr中

The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.
strtok和strtok_r函数返回指向下一个标记的指针,如果没有更多标记则返回NULL

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.
使用这些函数的时候需要小心,如果需要用到,注意如下:
这些函数会修改第一个参数
这些函数不能用于字符串常量
用于限定的字节将被丢失
strtok函数在解析的时候使用静态缓冲区,所以不是线程安全的,如果有必要请使用strtok_r

The  program  below  uses  nested loops that employ strtok_r() to break a string into a two-level hierarchy of tokens.  The first commandline argument specifies the string to be parsed.  The second argument specifies the delimiter byte(s) to be used to separate that string into  "major"  tokens.   The third argument specifies the delimiter byte(s) to be used to separate the "major" tokens into subtokens.
下面程序使用循环嵌套把字符串分成两个等级的标记,第一个命令行参数是用于解析的字符串,第二个参数是用来把字符串解析成major标记的限定符,第三个参数是用来把major标记解析成子标记的限定符


关于strtok的一个官方的例子如下:

       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


#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);
}

testcase如下:


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

int main(void)
{
	char str[100] = " my name is    cheny  ";
	const char *delim = " ";
	char *tmp = NULL, *dup = NULL;
	char *saveptr;

	dup = strdup(str);

	// strtok
	tmp = strtok(dup, delim);
	printf("tmp = %s\n", tmp);
	while(tmp = strtok(NULL, delim)) {
		printf("tmp = %s\n", tmp);
	}

	// strtok_r
	tmp = strtok_r(str, delim, &saveptr);
	printf("\n\ntmp = %s\n", tmp);
	printf("saveptr = %s\n", saveptr);
	while(tmp = strtok_r(NULL, delim, &saveptr)) {
		printf("tmp = %s\n", tmp);
		printf("saveptr = %s\n", saveptr);
	}
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
tmp = my
tmp = name
tmp = is
tmp = cheny


tmp = my
saveptr = name is    cheny  
tmp = name
saveptr = is    cheny  
tmp = is
saveptr =    cheny  
tmp = cheny
saveptr = 
可以看到strtok和strtok_r都可以把字符串解析成一系列token,限定符是" ",saveptr的作用是存储解析过后的字符串,因为在以后的调用中strtok的第一个参数都是NULL,所以需要一个指针来保存解析过后的字符串,以备下一次解析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值