学习strtok()和strsep()

学习strtok()和strsep()

函数原型:
char *strtok(char *s, const char *delim);
char *strsep(char **s, const char *delim);

/* strtok()源码 */
#include <string.h>

static char *olds;

#undef strtok
char * strtok (char *s,const char *delim)
{
  char *token;

  if (s == NULL)
    s = olds;

  /* Scan leading delimiters. */
  s += strspn (s, delim); //将指针移到第一个非delim中的字符的位置(s字符串开头属于delim的字符会被跳过)
  if (*s == '\0')
    {
      olds = s;
      return NULL;
    }

  /* Find the end of the token. */
  token = s;
  s = strpbrk (token, delim);// 获取到delim中字符在字符串s(Scan已经将s前面属于delim的字符跳过)中第一次出现的位置
  if (s == NULL)
    /* This token finishes the string. */
    olds = __rawmemchr (token, '\0');
  else
    {
      /* Terminate the token and make OLDS point past it. */
      *s = '\0';
      olds = s + 1;/* 这个变量,作为下次使用strtok第一个参数设置为NULL所用 */
    }
  return token;
}

strtok()中s+=strspn(s,delim);将s指向第一个非delim中的字符的位置,下面举三种普遍情况:

char *delim = ",";
/* 1. */char s11[] = ",,,,,,,abc";
/* 2. */char s21[] = "abc,,def";
/* 3. */char s31[] = "abc,def";
char *s1 = s11,*s2 = s21,*s3 = s31;

第三种情况是最常见的,strtok(s3,delim);执行到strspn()之后,s3指向”abc,def”(第一个非delim的字符就是第一个字符)。
第一种情况,strtok(s1,delim);执行strspn()之后,s1指向”abc”(前面的一连串‘,’都被跳过了)。
第二种情况,strtok(s2,delim);执行strspn()之后,s2指向”abc,,def”,执行完strtok之后,返回字符串为”abc\0,def”,olds指向”,def“。第二次使用strtok(NULL,delim);参数为NULL,s=olds;这就指向了上一次调用之后存在变量olds里面的”,def“了,然后strspn()会将开头的”,”跳过。

总结,strtok()会跳过s中开头连续的属于delim的字符,多次调用strtok(),产生效果是,如果最开始原始的字符串存在非开头的连续的属于delim的字符,第一个属于delim的字符变成'\0',其他的字符被跳过。

char st[] = ",,,abc,,,def,,,g";
char *s = st;
char *delim = ",";
char *s1 = strtok(s,delim);/*s1为"abc\0,,def,,,g" olds为",,def,,,g"*/
char *s2 = strtok(NULL,delim);/*s2为"def\0,,g" olds为",,g"*/
char *s3 = strtok(NULL,delim);/*s3为"g" olds为NULL*/
/* 原字符串变为",,,abc\0,,def\0,,g"*/

Linux内核2.6.29说明了strtok()已经不再使用,由速度更快的strsep()代替。

/*
 * Get next token from string *stringp, where tokens are possibly-empty
 * strings separated by characters from delim.
 *
 * Writes NULs into the string at *stringp to end tokens.
 * delim need not remain constant from call to call.
 * On return, *stringp points past the last NUL written (if there might
 * be further tokens), or is NULL (if there are definitely no moretokens).
 *
 * If *stringp is NULL, strsep returns NULL.
 */
char *strsep(char **stringp, const char *delim)
{
    char *s;
    const char *spanp;
    int c, sc;
    char *tok;
    if ((s = *stringp)== NULL)
        return (NULL);
    for (tok = s;;) {
        c = *s++;
        spanp = delim;
        do {
            if ((sc =*spanp++) == c) {
                if (c == 0)
                    s = NULL;
                else
                    s[-1] = 0;
                *stringp = s;
                return (tok);
            }
        } while (sc != 0);
    }
    /* NOTREACHED */
}
char *delim = ",";
/* 1. */char s11[] = ",,,,,,,abc";
/* 2. */char s21[] = "abc,,def";
/* 3. */char s31[] = "abc,def";
char *s1 = s11,*s2 = s21,*s3 = s31;
char *token;
int i = 1;
for(token = strsep(&s1,delim); token!=NULL ; token = strsep(&s1,delim))
{
    printf(token);
    printf("%d",i);
    i++;
}
printf("\n");
i = 1;
for(token = strsep(&s2,delim); token!=NULL ; token = strsep(&s2,delim))
{
    printf(token);
    printf("%d",i);
    i++;
}
printf("\n");
i = 1;
for(token = strsep(&s3,delim); token!=NULL ; token = strsep(&s3,delim))
{
    printf(token);
    printf("%d",i);
    i++;
}
printf("\n");
return 0;
1234567abc8
abc12def3
abc1def2

对于连续字符属于delim,处理与strtok()不同的是,strtok()将第一个非开头的属于delim的字符置为’\0’,其余的跳过不做处理,而strsep不论是否开头是否连续,会处理每一个属于delim中字符的字符,置为’\0’,如果调用的时候,第一个字符就是属于delim的字符,相当于返回一个空串”“,指针指向下一个字符。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值