Pointers on C——9 Strings, Characters, and Bytes.8

9.6.2 Finding Tokens

A string often contains several individual parts that are somehow separated from each other. To process these parts one at a time, you must first extract them from the string.

一个字符串常常包含几个单独的部分,它们彼此被分隔开来。每次为了处理这些部分,你首先必须把它们从字符串中抽取出来。


This task is exactly what the strtok function accomplishes. It isolates individual parts, called tokens, from a string and discards the separators. Its prototype is:

这个任务正是strtok 函数所实现的功能。它从字符串中隔离各个单独的称为标记(token) 的部分,并丢弃分隔符。它的原型如下:


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


The sep argument is a string that defines the set of characters that are used as separators. The first argument specifies a string that is assumed to contain zero or more tokens separated from one another by one or more characters from the sep string. strtok finds and NUL‐terminates the next token in str, and returns a pointer to the token.

sep 参数是个字符串,定义了用作分隔符的字符集合。第1 参数指定一个字符串,它包含零个或多个由sep 字符串中一个或多个分隔符分隔的标记。strtok 找到str的下一个标记,并将其用NUL结尾,然后返回一个指向这个标记的指针。


While it is doing its work, strtok modifies the string that it is processing. If the string must not be changed, copy it and use strtok on the copy.

当strtok 函数执行任务时,它将会修改它所处理的字符串。如果源、字符串不能被修改,那就复制一份,将这份拷贝传递给strtok 函数。


If the first argument to strtok is not NULL, the function finds the first token in the string, strtok also saves its position in the string. If the first argument to strtok is NULL, the function uses the saved position to find the next token from the same string as before. strtok returns a NULL pointer when there arenʹt any more tokens in the string. Typically, a pointer to a string is passed on the first call to strtok. The function is then called repeatedly with NULL first argument until it returns NULL.

如果strtok 函数的第1 个参数不是NULL.函数将找到字符串的第1 个标记。strtok 同时将保存它在字符串中的位置。如果strtok 函数的第1 个参数是NULL ,函数就在同一个字符串中从这个被保存的位置开始像前面一样查找下一个标记。如果字符串内不存在更多的标记,strtok 函数就返回一个NULL 指针。在典型情况下,在第1 次调用strtok 时,向它传递一个指向字符串的指针。然后,这个函数被重复调用(第1 个参数为NULL) ,直到它返回NULL 为止。


Program 9.3 is a short example. This function extracts tokens from its argument and prints them one per line. The tokens are separated by white space. Do not be confused by the appearance of the for statement. It was broken onto three lines because of its length.

程序9.3 是一个简短的例子。这个函数从它的参数中提取标记并把它们打印出来(一行一个)。这些标记用空白分隔。不要被for 语句的外观所混淆。它之所以被分成3 行是因为它实在太长了。

/*

** Extract whitespace-delimited tokens from a character array and

** print them one per line.

*/

#include <stdio.h>

#include <string.h>

void

print_tokens( char *line )

{

static char whitespace[] = " \t\f\r\v\n";

char *token;

for( token = strtok( line, whitespace );

token != NULL;

token = strtok( NULL, whitespace ) )

printf( "Next token is %s\n", token );

}

Program 9.3 Extract tokens


If you wish, you may use different separator sets in each call to strtok. This technique is handy when different parts of a string are separated by different groups of characters.

如果你愿意,你可以在每次调用strtok 函数时使用不同的分隔符集合。当一个字符串的不同部分由不同的字符集合分隔的时候,这个技巧根管用。


Because strtok saves local state information about the string it is parsing, you cannot use it to parse two strings concurrently. Thus, Program 9.3 would fail if the body of the for loop called a function that also called strtok.

由于strtok 函数保存它所处理的函数的局部状态信息,所以你不能用它同时解析两个字符串。因此,如采for 循环的循环体内调用了一个在内部调用strtok 函数的函数,程序9.3 将会失败。

上一章 Pointers on C——9  Strings, Characters, and Bytes.7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值