是否有 strsep() 函数的 Windows 变体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8512958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:39:49  来源:igfitidea点击:

Is there a Windows variant of strsep() function?

cwindows

提问by ProfessionalAmateur

I'm trying to parse a delimited string that has some emptyparameters.

我正在尝试解析具有一些参数的分隔字符串。

Example:

例子:

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

Basically I need to be able to pull out any segment by id, and if the segment is empty return null.

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

strtok()doesn't handle the empty fields, and it looks like there is strsep()for *nix based systems. Anyone know if there is something similar for Windows? I want to try and avoid having to write a function to handle this if I can.

strtok()不处理空字段,看起来像strsep()基于 *nix 的系统。有谁知道Windows是否有类似的东西?如果可以的话,我想尽量避免编写一个函数来处理这个问题。

回答by Alexey Frunze

Just write the function using its description, it's not terribly complex:

只需使用其描述编写函数,它并不是非常复杂:

#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 = '##代码##';
    *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;
}

回答by Brendan Shanks

There is a public-domain strsep()implementation located at http://unixpapa.com/incnote/string.htmlthat I've used before. It requires strcspn()though, a C90 function that you might not have.

我以前使用过strsep()位于http://unixpapa.com/incnote/string.html的公共域实现。strcspn()但是,它需要一个您可能没有的 C90 功能。

回答by Martin Beckett

Gnu have a port of some of the glibc functions to windows

Gnu 有一些glibc 函数到 windows的端口