约 1186 字

约 6 分钟

#C 语言标准库函数 wcstok

#include <wchar.h>

wchar_t *wcstok(wchar_t *restrict s, const wchar_t *restrict delim, wchar_t **restrict state);

说明

在字符串中提取由分隔符集合分开的非空字段。

相邻分隔符被跳过,不产生空字段。字符串会被就地改写,所以示例使用可写数组。

自 C95 起提供。

参数

  • s - 首次调用传可写字符串,后续调用传空指针。
  • delim - 作为分隔符的字符集合,以空字符结束。
  • state - 保存继续扫描位置的可写指针对象。

返回值

  • 找到字段时返回其起始地址,并在结束分隔符处写入终止符。到达末尾时返回空指针。

#示例

#include <wchar.h>
#include <stddef.h>

int main(void)
{
    wchar_t text[] = L"A,,B";
    wchar_t *state;
    wchar_t *first = wcstok(text, L",", &state);
    wchar_t *second = wcstok(NULL, L",", &state);
    return first == NULL || second == NULL || first[0] != L'A' || second[0] != L'B';
}

#相关接口

#参考标准

更新: 2026/11/28

作者: planc

创建: 2026/11/28