首页 > 编程知识 正文

strsplit函数的用法,strcat_s函数用法

时间:2023-05-05 10:51:25 阅读:241764 作者:1608

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

    free(string);

    return 0;

}

 

运行结果:

/home/yinlijun/project

/home/yinlijun

/home/someone

 

 

-----------------------------------------------OVER------------------------------------------

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。