ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

strtok()函数介绍

2022-01-27 21:01:35  阅读:225  来源: 互联网

标签:调用 函数 strtok 介绍 token strToken 字符串 NULL


作用:Find the next token(标志) in a string.

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 encounter

char *strToken

String containing token(s)

此字符指针指向一个字符串,它包含了0个或多个strDelimit字符串中的分隔符分割的标志(token)

const char *strDelimit

Set of delimiter characters

一串用于分割字符串的符号

On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

在第一次调用strtok时,函数跳过前导分隔符并返回指向strToken中的第一个令牌的指针,该令牌以一个空字符结束。通过对strtok的一系列调用,可以从其余的strToken中拆分出更多的令牌。对strtok的每次调用都通过在该调用返回的令牌之后插入一个空字符来修改strToken。要从strToken中读取下一个token,调用strtok时strToken参数的值为NULL。NULL strToken参数导致strtok在修改后的strToken中搜索下一个token。strDelimit参数可以从一个调用到下一个调用接受任何值,因此分隔符的集合可能不同。

在函数第一次调用时,第一个传参使用的是需要被分割的字符串。之后,每次调用都应该用NULL。在源字符串中,程序会自动将strToken中出现的strDelimit中的字符改为\0。注意,这会改变原字符串,如果只是想把字符串分割打印,可以拷贝一份字符串作为传参。当字符串仍有标志时,函数每次返回的都是字符串的地址。当字符串中不存在更多的标志时,返回的是NULL指针。

 第一次调用函数用的是字符串的地址,进入循环后,只要返回的不是NULL指针,就会继续打印切割字符串。直到返回NULL。

 

标签:调用,函数,strtok,介绍,token,strToken,字符串,NULL
来源: https://blog.csdn.net/The_west18/article/details/122723303

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有