strtok
来源:互联网 发布:司马懿之军事联盟 知乎 编辑:程序博客网 时间:2024/06/18 07:19
function
<cstring>
strtok
char * strtok ( char * str, const char * delimiters );
Split string into tokens
A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from thisbeginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.
This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function.
Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.
The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).
Parameters
- str
- C string to truncate.
Notice that this string is modified by being broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended. - delimiters
- C string containing the delimiter characters.
These may vary from one call to another.
Return Value
A pointer to the last token found in string.A null pointer is returned if there are no tokens left to retrieve.
Example
1234567891011121314151617
/* strtok example */#include <stdio.h>#include <string.h>int main (){ char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0;}
Output:
Splitting string "- This, a sample string." into tokens:Thisasamplestring
0 0
- strtok
- strtok
- strtok
- strtok()
- strtok
- strtok
- strtok
- strtok
- strtok
- strtok
- strtok
- strtok()
- strtok
- strtok
- strtok
- strtok
- strtok
- strtok
- Context initialization failed
- Unity3d 4.3.4f1运行项目
- 今天学习的内容dom4j编程、debug调试、JDK5.0新特性
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gditc.mmms/com.gditc.mm
- 为什么要有标题啊
- strtok
- Java Class文件结构
- MFC TAD Control 控件的使用
- 【Leetcode】Longest Palindromic Substring Part
- 黑马程序员--java关键字的一些区别
- 利用PowerDesigner15在win7系统下对MySQL 进行反向工程(三)
- PostThreadMessage
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- Linux下基于Erlang的高并发TCP连接压力实验