代码之家  ›  专栏  ›  技术社区  ›  Bash

用strtok解析字符串中的空标记

  •  4
  • Bash  · 技术社区  · 16 年前

    2342|2sd45|dswer|2342||5523|||3654|Pswt
    

    我正在使用 strtok 在一个循环中这样做。第五个标记是5523。但是,我需要考虑两个分隔符之间的空值 || 也。根据我的要求,5523应该是第六个代币。

    token = (char *)strtok(strAccInfo, "|");
    
    for (iLoop=1;iLoop<=106;iLoop++) { 
                token = (char *)strtok(NULL, "|");
    }
    

    7 回复  |  直到 6 年前
        1
  •  7
  •   Jonathan Leffler    11 年前

    那样的话,我通常更喜欢 p2 = strchr(p1, '|') 循环使用 memcpy(s, p1, p2-p1) const char * )而且非常便携(即使在嵌入式系统上)。

    它也是可重入的; strtok 不是(顺便说一句:可重入与多线程无关。 斯特托克 strtok_r 但它没有那么便携。)

        2
  •  2
  •   Gilles 'SO- stop being evil'    16 年前

    这是一个限制 strtok . 设计师们考虑了用空格分隔的标记。 斯特托克 C FAQ has an example .

        3
  •  2
  •   Romain Hippeau    16 年前

    作为str参数的C字符串,其 第一个字符用作 开始扫描令牌的位置。 在随后的调用中,函数 需要空指针并使用 令牌作为的新起始位置

    决定开始和结束 对于令牌,函数首先扫描 第一个字符不包含在 分隔符(成为 标记的开头)。然后 第一个字符的标记 令牌的结尾。

    由于您的数据是这样设置的,因此您有两种可能的解决方案:

        4
  •  2
  •   user411313    16 年前
    char *mystrtok(char **m,char *s,char c)
    {
      char *p=s?s:*m;
      if( !*p )
        return 0;
      *m=strchr(p,c);
      if( *m )
        *(*m)++=0;
      else
        *m=p+strlen(p);
      return p;
    }
    
    • 可重入的
    • 线程安全的
    • 调用时需要一个未使用的帮助指针 上下文

    例如

    char *p,*t,s[]="2342|2sd45|dswer|2342||5523|||3654|Pswt";
    for(t=mystrtok(&p,s,'|');t;t=mystrtok(&p,0,'|'))
      puts(t);
    

    char *p,*t,s[]="2,3,4,2|2s,d4,5|dswer|23,42||5523|||3654|Pswt";
    for(t=mystrtok(&p,s,'|');t;t=mystrtok(&p,0,'|'))
    {
      char *p1,*t1;
      for(t1=mystrtok(&p1,t,',');t1;t1=mystrtok(&p1,0,','))
        puts(t1);
    }
    

    你的作品:) 实现char*c作为参数3

        5
  •  1
  •   Chris    16 年前

    转而考虑使用strep: strsep reference

        6
  •  1
  •   Jerry Coffin    16 年前

    使用其他方法 strtok strcspn 或 strpbrk 我自己处理剩下的事情。如果您不介意的话,可以修改输入字符串 斯特托克 ,应该很简单。至少现在,这样的事情似乎应该奏效:

    // Warning: untested code. Should really use something with a less-ugly interface.
    char *tokenize(char *input, char const *delim) { 
        static char *current;    // just as ugly as strtok!
        char *pos, *ret;
        if (input != NULL)
            current = input;
    
        if (current == NULL)
            return current;
    
        ret = current;
        pos = strpbrk(current, delim);
        if (pos == NULL) 
            current = NULL;
        else {
            *pos = '\0';
            current = pos+1;
        }
        return ret;
    }
    
        7
  •  1
  •   Accountant Ù    7 年前

    灵感来自 Patrick Schlüter answer

    char* strTok(char** newString, char* delimiter)
    {
        char* string = *newString;
        char* delimiterFound = (char*) 0;
        int tokLenght = 0;
        char* tok = (char*) 0;
    
        if(!string) return (char*) 0;
    
        delimiterFound = strstr(string, delimiter);
    
        if(delimiterFound){
            tokLenght = delimiterFound-string;
        }else{
            tokLenght = strlen(string);
        }
    
        tok = malloc(tokLenght + 1);
        memcpy(tok, string, tokLenght);
        tok[tokLenght] = '\0';
    
        *newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;
    
        return tok;
    }
    

    你可以像这样使用它

    char* input = "1,2,3,,5,";
    char** inputP = &input;
    char* tok;
    while( (tok=strTok(inputP, ",")) ){
        printf("%s\n", tok);
    }
    

    这应该是输出

    1
    2
    3
    
    5
    

    code review

        8
  •  0
  •   Jonathan Leffler    10 年前

    下面是现在对我有效的解决方案。感谢你们所有的回应。

    我正在使用LoadRunner。因此,一些不熟悉的命令,但我相信流程可以很容易理解。

    char strAccInfo[1024], *p2;
    int iLoop;
    
    Action() {  //This value would come from the wrsp call in the actual script.
        lr_save_string("323|90||95|95|null|80|50|105|100|45","test_Param");
    
        //Store the parameter into a string - saves memory. 
        strcpy(strAccInfo,lr_eval_string("{test_Param}"));
        //Get the first instance of the separator "|" in the string
        p2 = (char *) strchr(strAccInfo,'|');
    
        //Start a loop - Set the max loop value to more than max expected.
        for (iLoop = 1;iLoop<200;iLoop++) { 
    
            //Save parameter names in sequence.
            lr_param_sprintf("Param_Name","Parameter_%d",iLoop);
    
            //Get the first instance of the separator "|" in the string (within the loop).
            p2 = (char *) strchr(strAccInfo,'|');           
    
            //Save the value for the parameters in sequence. 
            lr_save_var(strAccInfo,p2 - strAccInfo,0,lr_eval_string("{Param_Name}"));   
    
            //Save string after the first instance of p2, as strAccInfo - for looping.
            strcpy(strAccInfo,p2+1);
    
            //Start conditional loop for checking for last value in the string.
            if (strchr(strAccInfo,'|')==NULL) {
                lr_param_sprintf("Param_Name","Parameter_%d",iLoop+1);
                lr_save_string(strAccInfo,lr_eval_string("{Param_Name}"));
                iLoop = 200;    
            }
        }
    }