是否有strsep()的窗口变体

我试图分析一个空格参数的分隔string。 例:

"|One|two|three||octopus|garbagecan||cartwheel||||" 

基本上我需要能够通过ID拉出任何段,如果段为空返回null

strtok不处理空字段,它看起来像是基于* nix的系统的strsep() 。 任何人都知道,如果有什么类似的Windows? 我想尽量避免编写一个函数来处理这个问题。

Solutions Collecting From Web of "是否有strsep()的窗口变体"

只需使用描述来编写函数,这不是非常复杂:

 #include <stddef.h> #include <string.h> #include <stdio.h> char* mystrsep(char** stringp, const char* delim) { char* start = *stringp; char* p; p = (start != NULL) ? strpbrk(start, delim) : NULL; if (p == NULL) { *stringp = NULL; } else { *p = '\0'; *stringp = p + 1; } return start; } // Test adapted from http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html. int main(void) { char string[] = "words separated by spaces -- and, punctuation!"; const char delimiters[] = " .,;:!-"; char* running; char* token; #define PRINT_TOKEN() \ printf("token: [%s]\n", (token != NULL) ? token : "NULL") running = string; token = mystrsep(&running, delimiters); /* token => "words" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "separated" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "by" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "spaces" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "and" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "punctuation" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => "" */ PRINT_TOKEN(); token = mystrsep(&running, delimiters); /* token => NULL */ PRINT_TOKEN(); return 0; } 

有一个公共领域的strsep()实现位于http://unixpapa.com/incnote/string.html我以前使用过&#x3002; 它需要strcspn()尽管你可能没有C90的功能。

Gnu有一些glibc函数的窗口