strspn&strcspn

size_t strspn (const char *s,const char * accept);
strspn返回s中第一个不在accept中出现过的字符下标。
Returns an integer value specifying the length of the substring in str that consists entirely of characters in strCharSet.
If str begins with a character not in strCharSet, the function returns 0.
accept可以包含多个字符,从str开始搜索,计算str中包含的accept中任一字符的下标。
#include <string.h>
#include <stdio.h>
main()
{
    char *str="Linux was first developed for 386/486-based pcs.";
    printf("%d\n",strspn(str,"Linux"));
    printf("%d\n",strspn(str,"/-"));
    printf("%d\n",strspn(str,"1234567890"));
}

运行结果:

5

0

0

 
size_t strcspn(const char *s, const char * reject);
strcspn()从参数s 字符串的开头计算连续的字符, 而这些字符都完全不在参数reject 所指的字符串中.
简单地说, 若strcspn()返回的数值为n, 则代表字符串s 开头连续有n 个字符都不含字符串reject 内的字符.
返回字符串s 开头连续不含字符串reject 内的字符数目,也即返回s中第一个在reject中出现过的字符下标。
#include <string.h>
 
main(void)
 
{
 
    char *str = "Linux was first developed for 386/486-based pcs. ";
 
    printf("%d\n", strcspn(str, " "));
 
    printf("%d\n", strcspn(str, "/-"));
 
    printf("%d\n", strcspn(str, "1234567890"));
 
}
 

运行结果:

5

33

30

编程使用:key-value去掉参数前面的空格

int _nvram_init(void *unused)
{
    FILE *in;
    char buffer[SIZE], *token, *line;
    int i;
    if (!(in = fopen(PATH_DEV_NVRAM, "r"))) {   
        printf("nvram file can not open\n");
        return -1; 
    } 
    while (fgets(buffer, 300, in)) {
        if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
        token = buffer + strspn(buffer, " ");
        if (*token == '\0') continue;
        line = token + strcspn(token, "=");
        if (*line == '\0') continue; *line = '\0'; line++; /* eat leading whitespace */ line = line + strspn(line, " "); /* eat trailing whitespace */ for (i = strlen(line); i > 0 && (line[i - 1]==' '); i--); line[i] = '\0'; _nvram_set(token,line);  } fclose(in); printf("nvram init----\n");

return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值