strtok函数的妙用,分割字符串
2015-01-13 11:25
561 查看
strtok分割字符串函数,很好的解决了字符分割的要求,不必遍历取关键字再区后面字符
这样字符串中查找关键值获取后面的东西就方便多了
//加啊如头文件
参数含义
str :: 第一次操作时原始字符串,当strtok分割一次成功后 ,设置为 NULL 继续扫描下面的字符 知道为空
简单的例子如下
运行结果
根据结果分析得出
字符串呗 ." ,-"这三个字符分割了
Otherwise, a null pointer.
A null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.
这样字符串中查找关键值获取后面的东西就方便多了
#include <string.h>
//加啊如头文件
char * strtok ( char * str, const char * delimiters );
参数含义
str :: 第一次操作时原始字符串,当strtok分割一次成功后 ,设置为 NULL 继续扫描下面的字符 知道为空
delimiters :: 标记字符 分割的中间值如 xiaowan#xiaoli 符号#
简单的例子如下
/* 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; } | Edit & Run |
Splitting string "- This, a sample string." into tokens: This a sample string
根据结果分析得出
字符串呗 ." ,-"这三个字符分割了
Return Value
If a token is found, a pointer to the beginning of the token.Otherwise, a null pointer.
A null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.
相关文章推荐
- strtok函数(分割字符串)
- LoadRunner中使用strtok函数分割字符串
- 标准C中strtok函数分割字符串
- strtok函数,分割字符串
- strtok函数分割字符串
- c++ 字符串分割一、用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串。 参数说明:
- C语言分割字符串strtok函数
- PAT乙级 1009. 说反话 (20) 句子中单词逆序输出,'\0'的妙用,字符串分割
- C语言利用strtok函数实现字符串自由分割
- strtok函数字符串分割
- 使用strtok函数分割字符串
- 标准C中strtok函数分割字符串
- 【摘录】C语言中利用 strtok函数进行字符串分割
- C 语言 分割字符串 strtok函数
- strtok函数分割字符串
- strtok函数实现字符串分割
- C/C++_字符串分割strtok函数
- 用strtok函数分割字符串
- 字符串分割
- Swift字符串分割——多重分隔符