如何解析 ','使用c分隔字符串?

发布于 2024-08-16 11:45:33 字数 875 浏览 5 评论 0原文

解析逗号分隔列表的最简单方法是什么,其中每个标记之间可以有零个元素。 cstring 可能看起来像

1, 3, 4, 5, 6, 7, 8, ....

But 也可能看起来像

, , , , , , , , , ...

我尝试过类似的东西:

char *original = "1, 3, 4, 5, 6, 7, 8, ...."
char *tok = strtok(original," ,")
while(tok!=NULL){
    while(*tok!='\0'){
      //dostuff
      tok++;
    }
tok=strtok(NULL," ,");
}

这显然只适用于逗号之间有元素的情况,例如我注意到如果没有元素,第一个项目列表将被跳过。

我尝试过其他解决方案,例如 strchr(),但这变得非常丑陋,我认为有一种更简单的方法。

谢谢

更新:

经过一些测试,我注意到“,”上的标记化似乎在所有情况下都有效,除非第一个项目丢失。所以我把它作为一个特例拿出来。

char *original = "1, 3, 4, 5, 6, 7, 8, ...."
if(*original==',')
  //dostuff    
char *tok = strtok(original,",")
while(tok!=NULL){
    while(*tok!='\0'){
      //dostuff
      tok++;
    }
tok=strtok(NULL,",");
}

感谢您的投入和帮助。 (也许我应该在发帖之前更仔细地考虑一下。)

What is the easiest way of parsing a comma separated list, where there can be zero elements between each token. The cstring could look like

1, 3, 4, 5, 6, 7, 8, ....

But could also look like

, , , , , , , , , ...

I've tried something like:

char *original = "1, 3, 4, 5, 6, 7, 8, ...."
char *tok = strtok(original," ,")
while(tok!=NULL){
    while(*tok!='\0'){
      //dostuff
      tok++;
    }
tok=strtok(NULL," ,");
}

This apparently only works, if there are elements between the comma's, for instance I've noticed that the first item list will be skipped if there are no elements.

I've tried other solutions like strchr(), but this gets very ugly, and I think there is an easier way.

Thanks

Update:

After some testing I noticed that tokenizing on "," seemed to work, on all cases except if the first item was missing. So I'm pulling that out as a special case.

char *original = "1, 3, 4, 5, 6, 7, 8, ...."
if(*original==',')
  //dostuff    
char *tok = strtok(original,",")
while(tok!=NULL){
    while(*tok!='\0'){
      //dostuff
      tok++;
    }
tok=strtok(NULL,",");
}

Thanks for your input and your help. (Maybe I should have given this a more careful thought before posting.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

套路撩心 2024-08-23 11:45:33

您可能想要研究非标准 strsep,它被设计为替代 strtok,它允许解析空字段。另请参阅 glibc 手册中有关 查找的章节字符串中的标记。它可在许多系统(各种 BSD、Linux、Mac OS X)上使用,但尚未标准化,因此我相信它可能不存在于 Windows 或 Solaris 上。

You might want to look into the nonstandard strsep, which is designed to be a replacement for strtok which allows parsing of empty fields. See also the glibc manual chapter on Finding Tokens in a String. It's available on many systems (various BSDs, Linux, Mac OS X), but is not standardized, so I believe it may not be present on Windows or Solaris.

離殇 2024-08-23 11:45:33

如果您需要做的只是忽略空“标记”,则可以使用 strspn 函数来检测仅包含空格的字符串。这是一个示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Is the given string whitespace only?
*/
int iswhitespace(char* s)
{
    return (strspn(s, " \t") == strlen(s));
}


int main()
{
    char line[] = "1, , 3, 4, 5, 6";
    char sep[] = ",";
    char* tok;

    tok = strtok(line, sep);

    while (tok)
    {
        if (iswhitespace(tok))
            printf("empty token\n");
        else
            printf("new token: %s\n", tok);

        tok = strtok(0, sep);
    }

    return 0;
}

这里的关键思想是仅对逗号进行标记,而不是“,”,它会跳过第一个元素。然后可以单独处理空白。

当然,这仍然留下了 strtok 将跳过连续逗号的事实。如果这对您不利,则无法使用 strtok 并且必须采用其他解决方案。

If all you need to do is ignore empty "tokens", you can use the strspn function to detect whitespace-only strings. Here's an example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Is the given string whitespace only?
*/
int iswhitespace(char* s)
{
    return (strspn(s, " \t") == strlen(s));
}


int main()
{
    char line[] = "1, , 3, 4, 5, 6";
    char sep[] = ",";
    char* tok;

    tok = strtok(line, sep);

    while (tok)
    {
        if (iswhitespace(tok))
            printf("empty token\n");
        else
            printf("new token: %s\n", tok);

        tok = strtok(0, sep);
    }

    return 0;
}

The key idea here is to tokenize on a comma only, and not " ," which skips the first element. Whitespace can then be handled separately.

Of course this still leaves the fact that strtok will skip spans of consecutive commas. If this isn't good for you, you can't use strtok and will have to employ another solution.

小傻瓜 2024-08-23 11:45:33
strtok cannot cannot distinguish between `,` and `,,`.
strtok cannot cannot distinguish between `,` and `,,`.
一身骄傲 2024-08-23 11:45:33

一个简单的 for 循环怎么样?

for (int begin = 0; original[begin]; ) {
  int end = begin;
  while (original[end] && original[end] != ',')
    ++end;

  // do something with original[begin] through original[end-1]

  begin = end;
}

How about a simple for loop?

for (int begin = 0; original[begin]; ) {
  int end = begin;
  while (original[end] && original[end] != ',')
    ++end;

  // do something with original[begin] through original[end-1]

  begin = end;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文