strsep

strsep(),作为strtok的升级版,是一个很有用的字符串处理函数, 但是也有很多不方便的地方, 使用时需特别小心, 好在注意的事项都在 man strsep 里面有。如下:

 

#include <string.h>

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

 

Be cautious when using this function.  If you do use it, note that:

       * This function modifies its first argument.

       * This function cannot be used on constant strings.

       * The identity of the delimiting character is lost.

 

实例:

#include <string>

#include <stdio.h>

 

int main(int arg, const char *argv[])

{

    char* string = strdup( "/home/yinlijun/project:/home/yinlijun:/home/someone");          /*字符串不能为常量,所以strdup*/
    char* p;

    while((p = strsep(&string, ":")) != NULL)        /*第一个参数设为二级指针, 字符串中所有的第二个参数(子串)最后会被替代成‘/0’*/ 
    {
        printf("%s/n", p);
    }

    return 0;

}

 

运行结果:

/home/yinlijun/project

/home/yinlijun

/home/someone

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值