How to split a string into tokens by '&'
in C?
问问题
21494 次
4 回答
13
char *token;
char *state;
for (token = strtok_r(input, "&", &state);
token != NULL;
token = strtok_r(NULL, "&", &state))
{
...
}
于 2010-01-19T07:31:03.727 回答
9
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.
于 2010-01-19T07:34:45.013 回答
2
您可以使用 strok() 函数,如下例所示。
/// 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]);
于 2010-01-19T07:48:34.577 回答
0
对我来说,使用strtok()
函数既不直观也太复杂,所以我设法创建了自己的函数。作为参数,它接受要拆分的字符串、确定标记之间的空格的字符和表示找到的标记数量的指针(在循环中打印这些标记时很有用)。此功能的一个缺点是每个令牌的最大长度固定。
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 32
char **txtspt(const char *text, char split_char, int *w_count)
{
if(strlen(text) <= 1)
return NULL;
char **cpy0 = NULL;
int i, j = 0, k = 0, words = 1;
//Words counting
for(i = 0; i < strlen(text); ++i)
{
if(text[i] == split_char && text[i + 1] != '\0')
{
++words;
}
}
//Memory reservation
cpy0 = (char **) malloc(strlen(text) * words);
for(i = 0; i < words; ++i)
{
cpy0[i] = (char *) malloc(MAX_WORD_LEN);
}
//Splitting
for(i = 0; i < strlen(text) + 1; ++i)
{
if(text[i] == split_char)
{
cpy0[k++][j] = '\0';
j = 0;
}
else
{
if(text[i] != '\n') //Helpful, when using fgets()
cpy0[k][j++] = text[i]; //function
}
}
*w_count = words;
return cpy0;
}
于 2021-09-23T12:15:44.543 回答