strsep函数求解

iampengxiaojin 2011-10-08 04:48:33
请问:strsep函数中第二个参数,截取的字符串在使用时是否并不是看作一个整体来截取?而是只要原字符串匹配截取字符串中任意一个字符则截取?我遇到这样一个例子:有如下字符串,12 34 56 78 98 -gate_id 05 07 08 09 10 12 13 14 15 16 17 19 21 25 26 27 28 29 33 34 35 36 37 38 40 41 42 Td 43 44 45 46 48 49 50 51 52 53 54 55 56 58 70 71 72 74 75 76 78 79 81 T1 T2 T3 T4 T5 T6 T7 T8 T9 TA TB TC TD TE TF TG TH TI TJ TK TL TM TN TO TP TQ TR TS TT TU TV TW TX TY Tb W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 WJ WK YM -mer_id 880029 880056 880057 880060 880063 880079 880100 880106 880107 880138 880145 880232 880239 880263 880267 880297 880364 880374 880384 880390 880397 880401 880410 880412 880414 880425 880436 880446 880457 880464,使用函数strsep(&p, "mer_id"),p指向这串字符串存储的数组,结果就出现:函数返回12 34 56 78 98 -gat, p指向的则是剩下的_id 05 07开始的字符串。所以有此疑问!strsep并不是全匹配,只是有字符吻合就截取?

另外我想请问一下,我要截取从date_id到mei_id之间的字符串,我除了用strstr,分别获取这两个关键字的指针,然后用memcpy外,其他还有没有方法?sscanf有没有这种表达式可以来匹配?或者正则?谢谢
...全文
322 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-10-08
  • 打赏
  • 举报
回复
楼主没看仔细,delim参数里任意一个字符作为一个token,而不是说整个匹配delim才停止。

NAME
strsep - extract token from string

SYNOPSIS
#include <string.h>

char *strsep(char **stringp, const char *delim);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

strsep(): _BSD_SOURCE

DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in
the string *stringp, where tokens are delimited by symbols in the string delim. This token is terminated with a '\0' character
(by overwriting the delimiter) and *stringp is updated to point past the token. In case no delimiter was found, the token is
taken to be the entire string *stringp, and *stringp is made NULL.
iampengxiaojin 2011-10-08
  • 打赏
  • 举报
回复
自己顶一下,请大家帮忙解答一下啊,谢谢了。
iampengxiaojin 2011-10-08
  • 打赏
  • 举报
回复
不是很明白,我字符串里在42位置后面有一个Td, 函数就会截到这个d就停下来了,根本不是我想要的到mei_id的位置停下来啊
荒村归来 2011-10-08
  • 打赏
  • 举报
回复
百度百科解释如下:
原型:char *strsep(char **stringp, const char *delim);   功能:分解字符串为一组字符串。从stringp指向的位置起向后扫描,遇到delim指向位置的字符后,将此字符替换为NULL,返回stringp指向的地址。
按此解释,你的第二个问题貌似不需要那么麻烦吧

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧