如何从C中的字符串中提取子字符串?

我尝试使用strncmp但它只有在我给它一个特定数量的字节我想要提取时才有效。

char line[256] = This "is" an example. //I want to extract "is" char line[256] = This is "also" an example. // I want to extract "also" char line[256] = This is the final "example". // I want to extract "example" char substring[256] 

我如何提取“”之间的所有元素? 并把它放在变量substring?

注意:在我意识到编写代码会导致问题后我编辑了这个答案,因为strtok不喜欢操作const char*变量。 这更像是我编写示例的工件,而不是基本原理的问题 – 但显然它应该是双重downvote。 所以我修好了。

以下工作(使用gcc在Mac OS 10.7上测试):

 #include  #include  int main(void) { const char* lineConst = "This \"is\" an example"; // the "input string" char line[256]; // where we will put a copy of the input char *subString; // the "result" strcpy(line, lineConst); subString = strtok(line,"\""); // find the first double quote subString=strtok(NULL,"\""); // find the second double quote printf("the thing in between quotes is '%s'\n", subString); } 

以下是它的工作原理: strtok查找“delimiters”(第二个参数) – 在这种情况下,第一个" 。在内部,它知道”它到底有多远“,如果再次调用它,并将NULL作为第一个参数(而不是一个char* ),它将从那里再次开始。因此,在第二次调用时,它返回“正好是第一个和第二个双引号之间的字符串”。这就是你想要的。

警告: strtok通常用'\0'替换分隔符,因为它“吃掉”输入。 因此,您必须依靠通过此方法修改的输入字符串。 如果这是不可接受的,您必须先制作本地副本。 本质上,当我将字符串常量复制到变量时,我会在上面这样做。 通过调用line=malloc(strlen(lineConst)+1);来执行此操作会更干净line=malloc(strlen(lineConst)+1);free(line); 之后 – 但是如果你打算将它包装在一个函数中,你必须考虑返回值必须在函数返回后保持有效…因为strtok返回一个指向字符串内正确位置的指针,它不会使令牌的副本。 将指针传递到您希望结果结束的空间,并在函数内创建该空间(具有正确的大小),然后将结果复制到其中,这是正确的做法。 这一切都非常微妙。 如果不清楚,请告诉我!

你试过看strchr函数了吗? 您应该能够调用该函数两次以获取指向"字符"的第一个和第二个实例的指针,并使用memcpy和指针算法的组合来获得您想要的内容。

如果你想在没有图书馆支持的情况下做到这一点……

 void extract_between_quotes(char* s, char* dest) { int in_quotes = 0; *dest = 0; while(*s != 0) { if(in_quotes) { if(*s == '"') return; dest[0]=*s; dest[1]=0; dest++; } else if(*s == '"') in_quotes=1; s++; } } 

然后打电话给它

extract_between_quotes(line, substring);

 #include  ... substring[0] = '\0'; const char *start = strchr(line, '"') + 1; strncat(substring, start, strcspn(start, "\"")); 

省略了界限和错误检查。 避免strtok因为它有副作用。

这是一个很长的方法:假设要提取的字符串将在引号中(修复为kieth在下面的评论中建议的错误检查)

 #include  #include  #include  int main(){ char input[100]; char extract[100]; int i=0,j=0,k=0,endFlag=0; printf("Input string: "); fgets(input,sizeof(input),stdin); input[strlen(input)-1] = '\0'; for(i=0;i 

输出(1):

 $ ./test Input string: extract "this" from this string Extract = this 

输出(2):

 $ ./test Input string: Another example to extract "this gibberish" from this string Extract = this gibberish 

输出(3):( Kieth建议的错误检查)

$ ./test

 Input string: are you "happy now Kieth ? 1.Your code only had one quotation mark. 2.So the code extracted everything after that quotation mark 3.To make sure buffer overflow doesn't happen in this case: 4.Modify the extract buffer size to be the same as input buffer size extracted string: happy now Kieth ? 

-------------------------------------------------- -------------------------------------------------- ----------------------------

虽然没有要求它 - 以下代码从输入字符串中提取多个单词,只要它们在引号中:

 #include  #include  #include  int main(){ char input[100]; char extract[50]; int i=0,j=0,k=0,endFlag=0; printf("Input string: "); fgets(input,sizeof(input),stdin); input[strlen(input)-1] = '\0'; for(i=0;i 

输出:

 $ ./test Input string: extract "multiple" words "from" this "string" Extract = multiplefromstring