用连续的分隔符strtok_s行为 [英] strtok_s behaviour with consecutive delimiters

查看:675
本文介绍了用连续的分隔符strtok_s行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与特定的分离器分离并行分析3的值。

I'm parsing 3 values in parallel which are separated with a specific separator.

token1 = strtok_s(str1, separator, &nextToken1);
token2 = strtok_s(str2, separator, &nextToken2);
token3 = strtok_s(str3, separator, &nextToken3);

while ((token1 != NULL) && (token2 != NULL) && (token3 != NULL))
{
    //...
    token1 = strtok_s(NULL, separator, &nextToken1);
    token2 = strtok_s(NULL, separator, &nextToken2);
    token3 = strtok_s(NULL, separator, &nextToken3);
}

假设 - 是我的分隔符。该行为是没有连续的分隔符的字符串:

Suppose '-' is my separator. The behaviour is that a string with no consecutive separators:

1-2-3-45

将有效地导致在这些部分组成:

would effectively result in each of these parts:

1
2
3
45

然而,连续两个隔板的字符串:

However, a string with two consecutive separators:

1-2--3-45

不会产生一个0长度的字符串,那一个被跳过,使得结果是:

will not yield a 0 length string, that one is skipped so that the result is:

1
2
3
45

而不是

1
2

3
45

什么解决办法或策略会更适合获得所有实际的部分,包括0长度的呢?我想,以避免重新实现strtok_s,如果可能的话。

What workaround or strategy would be better suited to obtain all the actual parts, including the 0-length ones? I'd like to avoid re-implementing strtok_s, if possible.

推荐答案

不幸的是,的strtok()忽略空令牌。即使你说你希望避免这样做,有没有其他办法,只能自己解析它,例如使用和strchr()来找到下一个分隔符,然后复制令牌进行处理的临时变量。这样,您就可以处理空标记,无论怎样都可以。

Unfortunately, strtok() ignores empty tokens. Even though you said you wish to avoid doing that, there is no other way but to parse it yourself, using for example strchr() to find the next delimiter and then copying the token to a temporary variable for processing. This way you can handle empty tokens whichever way you please.

这篇关于用连续的分隔符strtok_s行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆