strtok返回值的问题

ProgrammingRing 2011-12-30 09:46:18
比如如此啊代码:
string的字符串为:hi/hello.world
str = strtok(string, "-");
那这个不是找不到标记就反悔NULL的嘛?怎么不是呢?非要在调用一次str = strtok(NULL, "-");才返回的是NULL啊,怎么回事呢?
...全文
264 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yafeng_jiang 2011-12-30
  • 打赏
  • 举报
回复
This character in the original
string newstring is overwritten by a null character, and the pointer to the beginning
of the token in newstring is returned.
JoeBlackzqq 2011-12-30
  • 打赏
  • 举报
回复
strtok, wcstok
Find the next token in a string.

Routine Required Header
strtok <string.h>
wcstok <string.h>


char *strtok( char *strToken, const char *strDelimit );

Return Value
All of these functions return 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.

Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok is the wide-character version of strtok. The arguments and return value of wcstok are wide-character strings. These two functions behave identically otherwise.

根据指定的分隔符分割字符串,但是当没有分隔符时,结果就正好是原串!
yafeng_jiang 2011-12-30
  • 打赏
  • 举报
回复
str = strtok(string, "-");
只是想要把“-”换为NULL,然后返回串首。
当调用strtok(NULL, "-");时才开始去截取,找不到则返回NULL
Gloveing 2011-12-30
  • 打赏
  • 举报
回复
/* 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
Gloveing 2011-12-30
  • 打赏
  • 举报
回复
/* 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
Gloveing 2011-12-30
  • 打赏
  • 举报
回复
/* 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

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧