How to split a string into tokens in C?

2020-02-01 02:14发布

How to split a string into tokens by '&' in C?

标签: c split token
3条回答
等我变得足够好
2楼-- · 2020-02-01 02:54

I would do it something like this (using strchr()):

#include <string.h>

char *data = "this&&that&other";
char *next;
char *curr = data;
while ((next = strchr(curr, '&')) != NULL) {
    /* process curr to next-1 */
    curr = next + 1;
}
/* process the remaining string (the last token) */

strchr(const char *s, int c) returns a pointer to the next location of c in s, or NULL if c isn't found in s.

You might be able to use strtok(), however, I don't like strtok(), because:

  • it modifies the string being tokenized, so it doesn't work for literal strings, or is not very useful when you want to keep the string for other purposes. In that case, you must copy the string to a temporary first.
  • it merges adjacent delimiters, so if your string was "a&&b&c", the returned tokens are "a", "b", and "c". Note that there is no empty token after "a".
  • it is not thread-safe.
查看更多
狗以群分
3楼-- · 2020-02-01 03:03

You can use the strok() function as shown in the example below.

/// Function to parse a string in separate tokens 

int parse_string(char pInputString[MAX_STRING_LENGTH],char *Delimiter,
                   char *pToken[MAX_TOKENS])
{
  int i;
  i = 0;

  pToken[i] = strtok(pInputString, Delimiter);
  i++;

  while ((pToken[i] = strtok(NULL, Delimiter)) != NULL){
     i++;
  }
  return i;
}

/// The array pTokens[] now contains the pointers to the start of each token in the (unchanged) original string.

sprintf(String,"Token1&Token2");
NrOfParameters = parse_string(String,"&",pTokens);

sprintf("%s, %s",pToken[0],pToken[1]);
查看更多
贪生不怕死
4楼-- · 2020-02-01 03:08

strtok / strtok_r

char *token;
char *state;

for (token = strtok_r(input, "&", &state);
     token != NULL;
     token = strtok_r(NULL, "&", &state))
{
    ...
}
查看更多
登录 后发表回答