且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何来标记含有用strtok_r空值的字符串

更新时间:2021-09-01 00:31:03

从strtok_r手册页:

From strtok_r man page:

的两个或更多个连续分隔符分析的字符串中的序列被认为是一个单一的分隔符。

A sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.

所以它不会在你的情况下工作。但是你可以用code像这样的:结果

So it won't work in your case. But you can use code like this one:

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

int main(void) {
    int i = 0;
    char result[512];
    char *str = result, *ptr;
    strcpy(result, "Hello,world,,,wow");
    while (1) {
        ptr = strchr(str, ',');
        if (ptr != NULL) {
            *ptr = 0;
        }
        printf("%d\n", ++i);
        printf("%s\n", str);
        if (ptr == NULL) {
            break;
        }
        str = ptr + 1;
    }
    return 0;
}