strtok&strsep
strtok,strtok_r,strsep--extract tokens from strings
Tje strsep() function was introduced as a replacement for strtok, since the latter cannot handle empty fileds. However, strtok conforms to C89/C99 and hence is more portable.
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr); 可重入
char *strsep(char **stringp, const char *delim);
根据delim字符串分解str。
saveptr用于保存每次调用完参数后下一次进行字符串分割时的起始地址,内部设定。
delim可为多个字符,每个字符都分解str,str中出现的不连续分解字符都改写为‘\0’, 连续的分割符被认为是一个,str中开始和结束的分隔符被忽略。
strtok使用:1)首次调用分割字符串参数为str,之后都为NULL。
2)返回NULL或分割字符串(不包含delim字符)
strtok_r使用:1)首次调用str指向要分割的字符串,saveptr忽略;接下来调用中str为NULL,saveptr应该不改变。
2)不同字符串可以同时被解析,指定不同saveptr参数。
使用注意:1)函数修改第一个参数str。
2)str不能是常量string。
3)strtok()用到静态buffer,因此非线程安全。
man中提供的一个例程
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(int argc, char **argv)
{
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int j; if(argc != )
{
fprintf(stderr, "Usage: %s string delim subdelim\n", argv[]);
exit(EXIT_FAILURE);
} for(j = , str1 = argv[]; ; j++, str1 = NULL)
{
token = strtok_r(str1, argv[], &saveptr1);
if(token == NULL)
{
break;
}
printf("%d: %s\n", j, token); for(str2 = token;; str2 = NULL)
{
subtoken = strtok_r(str2, argv[], &saveptr2);
if(subtoken == NULL)
{
break;
}
printf("\t --> %s\n", subtoken);
}
} exit(EXIT_FAILURE); return ;
}
~$./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
: a/bbb///cc
--> a
--> bbb
--> cc
: xxx
--> xxx
: yyy
--> yyy
strsep与strtok等同,与strtok相比,strsep可以处理空字符串。
If *stringp is NULL, the strsep() function returns NULL and does nothing else.
char *ptr;
char *now_ptr;
for (ptr = strsep(&now_ptr,";"); ptr != NULL; ptr = strsep(&now_ptr,";"))
strtok&strsep的更多相关文章
- C语言 字符串切割
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 字符串切割函数 */ /* 知识补充: ...
- strsep和strtok_r替代strtok
char *strtok(char *str, const char *delim) 会修改数据源.外部加锁才线程安全(strtok执行结束再解锁执行另一个strtok循环知道工作完成) 主要是以互斥 ...
- C/C++ 字符串分割: strtok 与 strsep 函数说明
函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...
- 【C】——strtok()和strtok_r()
下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()这个函数已经不再使用,由速度更快的strsep()代替 /** linux/lib/string.c** Copyright ( ...
- 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset
bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...
- loadrunner中切割strtok字符串
http://blog.sina.com.cn/s/blog_7ee076050102vamg.html http://www.cnblogs.com/lixiaohui-ambition/archi ...
- 字符串分割函数 STRTOK & STRTOK_R (转)
1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{ char name[25]; char sex[1 ...
- strtok和strtok_r
1.strtok()函数的用法 函数原型:char *strtok(char *s, const char *delim); Function:分解字符串为一组字符串.s为要分解的字符串,delim为 ...
- 浅谈strtok
原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组标记串.s为要分解的字符串,delim为分隔符字符串. 说明:首次调用时,s必须指向要分解的字符串, ...
随机推荐
- CentOS 6.5下源码安装MySQL 5.6
变量lower_case_file_system说明是否数据目录所在的文件系统对文件名的大小写敏感.ON说明对文件名的大小写不敏感,OFF表示敏感. 在my.cnf中[mysqld]更改lower_c ...
- JavaScript系列:函数调用方式
有关JS的问题,持续更新.. 一,函数调用的4种方式 1,函数调用模式 //下面这种模式叫 “函数调用模式”:窗后window来调用 //函数调用四种方式的基础 //这tm不就是作用域this的问题吗 ...
- 学会查看tomcat的日志文件
1.Tomcat的启动日志写出了几乎所有的启动历史记录, 包括部署项目,deploy项目.用了什么log日志记录软件,启动的tomcat引擎是什么,正在部署什么项目deploying
- php中json_decode()和json_encode()的使用方法
php中json_decode()和json_encode()的使用方法 作者: 字体:[增加 减小] 类型:转载 json_decode对JSON格式的字符串进行编码而json_encode对变 ...
- 页面静态化3 --- 伪静态技术之Apache的rewrite机制
Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...
- WPF 最大化最小化窗口
public static void FullOrMin(this Window window) { //如果是全屏,则最小化 if (win ...
- Nginx部署ThinkPHP项目的办法
thinkphp config配置: ', //URL模式 nginx rewrite配置: location / { if (!-e $request_filename) { rewrite ^(. ...
- taocode
http://code.taobao.org/project/lang/list/Go/1/
- Java程序设计的基本原则
Java程序设计的基本原则-1 1.面向对象 这是java编程里面大家公认的第一原则 2.优先使用对象组合而非类继承 3.分层 最典型的三层架构,表现层-->逻辑层-->数据层 表现层功能 ...
- 用户交互与while循环<代码>
#用户交互1 age_oldboy = 56 guess_age = int(input(">>:")) if guess_age == age_oldboy: pri ...