导航

strsep()函数

Posted on 2012-02-21 20:20  网名还没想好  阅读(1566)  评论(0编辑  收藏  举报

  原型:char *strsep(char **stringp, const char *delim);   

        功能:分解字符串为一组字符串。从stringp指向的位置起向后扫描,遇到delim指向位置的字符后,将此字符替换为NULL,返回stringp指向的地址。  

    strsep函数,这在 Windows Dev-C++ 是没有支持的,在写 UNIX 分析字符串常常需要利用到此函式,大家可以 man strsep来看如何使用 strsep,假设我们要分析 URL Get 字符串:user_command=appleboy&test=1&test2=2,就可以利用两次 strsep 函式,将字符串全部分离,取的个别的 name,value。strsep(stringp,delim) 第一个参数传入需要分析的字符串,第二个参数传入 delim 符号,假设 stringp 为 NULL 字符串,则函式会回传 NULL,换句话说,strsep 会找到 stringp 字符串第一个出现 delim 符号,并将其取代为 \0 符号,然后将 stringp 更新指向到 \0 符号的下一个字符串,strsep() function 回传原来的 stringp 指标。

例子:

#include   <string.h>
#include   <stdlib.h>
int   main()
{
char   ptr[]={ "abcdefghijklmnopqrstuvwxyz "};
char   *p,*str= "m ";
p=ptr;
printf( "%s\n ",strsep(&p,str));
printf( "%s\n ",p);
str= "s ";
printf( "%s\n ",strsep(&p,str));
printf( "%s\n ",p);

}


[root@shwhg   test]#   gcc   test131.c
[root@shwhg   test]#   ./a.out
abcdefghijkl
nopqrstuvwxyz
nopqr
tuvwxyz