• 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
    ",strspn(str,"Linux"));
        printf("%d
    ",strspn(str,"/-"));
        printf("%d
    ",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
    ", strcspn(str, " "));
     
        printf("%d
    ", strcspn(str, "/-"));
     
        printf("%d
    ", 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
    ");
            return -1; 
        } 
        while (fgets(buffer, 300, in)) {
            if (strchr(buffer, '
    ')) *(strchr(buffer, '
    ')) = '';
            token = buffer + strspn(buffer, " ");
            if (*token == '') continue;
            line = token + strcspn(token, "=");
            if (*line == '') continue;
            *line = '';
            line++;
    
            /* eat leading whitespace */
            line = line + strspn(line, " ");
            /* eat trailing whitespace */
            for (i = strlen(line); i > 0 && (line[i - 1]==' '); i--);
            line[i] = '';
            _nvram_set(token,line);
        }
        fclose(in);
        printf("nvram init----
    ");

    return 0;

    }
  • 相关阅读:
    【python】PyQt5 QAction 添加点击事件
    【环境搭建】安装pyQt5 在pycharm报This application failed to start because no Qt platform plugin could be initialized的问题
    【嵌入式】arduino IDE串口监视器可以正常使用但其他软件发送串口指令没有反应的问题
    【操作系统】bat文件 系统找不到文件路径
    十天冲刺:第二天
    十天冲刺:第一天
    项目需求分析NABCD电梯演讲
    项目需求分析与建议-NABCD模型
    团队开发之团队介绍
    会议1.7
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5281740.html
Copyright © 2020-2023  润新知