如何解析 ','使用c分隔字符串?
解析逗号分隔列表的最简单方法是什么,其中每个标记之间可以有零个元素。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要研究非标准
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 forstrtok
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.如果您需要做的只是忽略空“标记”,则可以使用
strspn
函数来检测仅包含空格的字符串。这是一个示例:这里的关键思想是仅对逗号进行标记,而不是“,”,它会跳过第一个元素。然后可以单独处理空白。
当然,这仍然留下了
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: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 usestrtok
and will have to employ another solution.一个简单的
for
循环怎么样?How about a simple
for
loop?