首页 > 其他 > 详细

strtok函数的妙用,分割字符串

时间:2015-01-13 12:32:54      阅读:158      评论:0      收藏:0      [点我收藏+]

strtok分割字符串函数,很好的解决了字符分割的要求,不必遍历取关键字再区后面字符

这样字符串中查找关键值获取后面的东西就方便多了

#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;
}

运行结果

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函数的妙用,分割字符串

原文:http://blog.csdn.net/wuheshi/article/details/42674231

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!