[英]How to use strtok to delimit and get new strings
I am trying to use strtok to break up a string and to store the extracted strings into new arrays so I can use them seperately as commands or something. 我正在尝试使用strtok分解字符串并将提取的字符串存储到新数组中,以便可以将它们分别用作命令或其他内容。
#include <stdio.h>
#include <string.h>
main()
{
char test_string[50]="string to split up";
char *sub_string;
/* Extract first string */
printf("%s\n", strtok(test_string, " "));
/* Extract remaining strings */
while ( (sub_string=strtok(NULL, " ")) != NULL)
{
printf("%s\n", sub_string);
}
}
This prints out the string I am looking for without the spaces, but how would I go about getting each of the words saved into seperate string variables instead of just printing them? 这样会打印出我要查找的字符串,不带空格,但是我将如何将每个单词保存到单独的字符串变量中,而不仅仅是打印它们? Basically I want "string" to be saved into string1[] and "to" to be saved in string2[] and so forth. 基本上,我希望将“字符串”保存到string1 []中,将“ to”保存到string2 []中,依此类推。
The original string gets "chopped into pieces" by the strtok
operation, so you can actually do the following: 原始字符串被strtok
操作“切成碎片”,因此您实际上可以执行以下操作:
#include <stdio.h>
#include <string.h>
int main(void)
{
char test_string[50]="string to split up";
char *sub_string[50];
int ii = -1;
/* Extract first string */
sub_string[++ii]=strtok(test_string, " "));
printf("%s\n", sub_string[0]);
/* Extract remaining strings */
while ( (sub_string[++ii]=strtok(NULL, " ")) != NULL)
{
printf("%s\n", sub_string[ii]);
}
}
Basically, strtok
inserts '\\0'
at the delimiter found, and returns a pointer to the start of the token. 基本上, strtok
在找到的定界符处插入'\\0'
,并返回指向令牌开头的指针。 So you don't actually need to allocate new memory to the sub_string
elements - just to the array. 因此,您实际上不需要将新的内存分配给sub_string
元素-只需分配给数组即可。 I set the number of elements of the array to 50; 我将数组的元素数设置为50; in reality you will want to make sure that your while
loop stops before you run out of space… 实际上,您将需要确保while
循环在空间不足之前停止运行……
A little diagram might help: 一张小图可能会有所帮助:
Original string: 原始字串:
s t r i n g t o s p l i t u p \0
After first call to strtok
: 在第一次调用strtok
:
s t r i n g \0 t o s p l i t u p \0
^ first pointer
After next call: 下次通话后:
s t r i n g \0 t o \0 s p l i t u p \0
^ second pointer
After the third call: 第三次通话后:
s t r i n g \0 t o \0 s p l i t \0 u p \0
^ third pointer
etc. 等等
If you want to store the substrings in "different variables" (not sure why you don't consider sub_string[0]
and sub_string[1]
etc to be convenient 'different variables', but I'll leave that for another time), you could change the above to: 如果您想将子字符串存储在“不同变量”中(不知道为什么您不认为sub_string[0]
和sub_string[1]
等是方便的“不同变量”,但我会再留sub_string[1]
),您可以将以上内容更改为:
#include <stdio.h>
#include <string.h>
int main(void)
{
char test_string[50]="string to split up";
char *string1, *string2, *string3, *string4, *string5;
int n = 0;
/* Extract first string */
if(strlen(test_string)==0) return 0;
string1=strtok(test_string, " "));
n++;
printf("%s\n", string1);
while( n < 5 ) {
string2 = strtok(NULL, " ");
if (string2 == NULL) break; else n++;
string3 = strtok(NULL, " ");
if (string3 == NULL) break; else n++;
string4 = strtok(NULL, " ");
if (string4 == NULL) break; else n++;
string5 = strtok(NULL, " ");
if (string4 == NULL) break; else n++;
}
printf("the total number of strings found is %d\n", n);
}
I just don't think it is nearly as elegant as using an array of char *
. 我只是觉得这不像使用char *
数组那样优雅。 Can you see my point? 你明白我的意思吗?
#include <stdio.h>
#include <string.h>
main()
{
char test_string[50]="string to split up";
char *sub_string;
char withoutspace[50];
/* Extract first string */
printf("%s\n", strtok(test_string, " "));
/* Extract remaining strings */
while ( (sub_string=strtok(NULL, " ")) != NULL)
{
strcat(withoutspace, sub_string); //printf("%s\n", sub_string);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.