本文介绍了当一行中有 2 个分隔符时,将字符串拆分为 C 中的 Tokens的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 strtok() 函数将字符串拆分为令牌.问题是当行中有 2 个分隔符时.

I am using strtok() function to split a string into Tokens.The problem is when there are 2 delimiters in row.

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,", ");
  while (pch != NULL)
  {
    printf ("Token = %s\n",pch);
    pch = strtok (NULL, ", ");
  }
  return 0;
}

和输出:

Splitting string "Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,," into tokens:
Token = Test=
Token = 0.28
Token = 0.0
Token = 1
Token = 1.9
Token = 2.2
Token = 1.0
Token = 8
Token = 4
Token = 42

有一些简单的方法可以获取所有标记;我需要知道分隔符内是否有内容,因为有时我会得到 ,, 或 ,xxx,

There some easy way to get all tokens;I need to know if there's something inside delimiters cause there's times i get ,, or ,xxx,

谢谢.

推荐答案

strtok() 与您想要的完全相反.

strtok() does explicitly the opposite of what you want.

在在线手册中找到:

解析的两个或多个连续分隔符字节的序列string 被认为是单个分隔符.分隔符字节在字符串的开头或结尾将被忽略.换句话说:代币strtok() 返回的总是非空字符串.

strtok(3) - Linux 手册页

我实现了 strtok() - strtok() 的一个变体,它的行为类似但可以满足您的要求:

I implemented strtoke() - a variant of strtok() which behaves similar but does what you want:

/* strtoke example */
#include <stdio.h>
#include <string.h>

/* behaves like strtok() except that it returns empty tokens also
 */
char* strtoke(char *str, const char *delim)
{
  static char *start = NULL; /* stores string str for consecutive calls */
  char *token = NULL; /* found token */
  /* assign new start in case */
  if (str) start = str;
  /* check whether text to parse left */
  if (!start) return NULL;
  /* remember current start as found token */
  token = start;
  /* find next occurrence of delim */
  start = strpbrk(start, delim);
  /* replace delim with terminator and move start to follower */
  if (start) *start++ = '\0';
  /* done */
  return token;
}

int main ()
{
  char str[] ="Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtoke(str,", ");
  while (pch != NULL)
  {
    printf ("Token = %s\n",pch);
    pch = strtoke(NULL, ", ");
  }
  return 0;
}

在 cygwin 上用 gcc 编译和测试:

Compiled and tested with gcc on cygwin:

$ gcc -o test-strtok test-strtok.c

$ ./test-strtok.exe 
Splitting string "Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,," into tokens:
Token = Test=
Token = 0.28
Token = 0.0
Token = 1
Token = 
Token = 1.9
Token = 2.2
Token = 1.0
Token = 
Token = 8
Token = 4
Token = 
Token = 
Token = 42
Token = 
Token = 

上面链接中的另一个引用:

Another cite from the above link:

使用这些功能时要小心.如果您确实使用它们,请注意:

  • 这些函数修改它们的第一个参数.
  • 这些函数不能用于常量字符串.
  • 分隔字节的标识丢失.
  • strtok() 函数在解析时使用静态缓冲区,因此它不是线程安全的.如果这对您很重要,请使用 strtok_r() .

这些问题也适用于我的 strtoke().

These issues apply to my strtoke() also.

这篇关于当一行中有 2 个分隔符时,将字符串拆分为 C 中的 Tokens的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:23