如何提取在C串子? [英] How to extract a substring from a string in C?

查看:125
本文介绍了如何提取在C串子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用STRNCMP但如果我给它我想提取字节的具体数量它只。

I tried using strncmp but it only works if I give it a specific number of bytes I want to extract.

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]

我将如何提取所有在的元素呢?并把它放在变量子?

How would I extract all the elements in between the ""? and put it in the variable substring?

推荐答案

注意:我编辑这个答案后,我意识到,作为写在code会导致一个问题,因为 strtok的不喜欢在为const char * 变量进行操作。这是更多的我是怎么写的比用的基本原则有问题的例子神器 - 但显然当之无愧的双重downvote。所以我固定它。

Note: I edited this answer after I realized that as written the code would cause a problem as strtok doesn't like to operate on const char* variables. This was more an artifact of how I wrote the example than a problem with the underlying principle - but apparently it deserved a double downvote. So I fixed it.

下面的工作(在Mac OS 10.7使用gcc测试):

The following works (tested on Mac OS 10.7 using gcc):

#include <stdio.h>
#include <string.h>

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的查找分隔符(第二个参数) - 在这种情况下,第一个。在内部,它知道它能走多远了,如果你用 NULL 再次调用它作为第一个参数(而不是字符* ),它将再次从该处开始,因此,在第二次调用返回。这是你想要的东西正是第一和第二双引号之间的字符串。

Here is how it works: strtok looks for "delimiters" (second argument) - in this case, the first ". Internally, it knows "how far it got", and if you call it again with NULL as the first argument (instead of a char*), it will start again from there. Thus, on the second call it returns "exactly the string between the first and second double quote". Which is what you wanted.

警告: strtok的通常替换分隔符与'\\ 0',因为它吃的输入。因此,必须依靠你的输入字符串过得去这种方法修改。如果这是不能接受的,你必须先制作一个本地副本。从本质上说我这样做,在上面时,我复制字符串常量给一个变量。这将是吸尘器到行=的malloc(strlen的(lineConst)+1)的调用来做到这一点; 免费(线); 之后 - 但是如果你打算换这一点,你必须考虑到的返回值必须保持有效的函数返回后...在函数内部因为 strtok的返回一个指向字符串内正确的地方,它不会使令牌的副本。指针传递到你想要的结果结束了空间,并创建在函数内部空间(用正确的大小),然后复制结果到它,将是做正确的事。这一切是相当微妙。让我知道这是不明确!

Warning: strtok typically replaces delimiters with '\0' as it "eats" the input. You must therefore count on your input string getting modified by this approach. If that is not acceptable you have to make a local copy first. In essence I do that in the above when I copy the string constant to a variable. It would be cleaner to do this with a call to line=malloc(strlen(lineConst)+1); and a free(line); afterwards - but if you intend to wrap this inside a function you have to consider that the return value has to remain valid after the function returns... Because strtok returns a pointer to the right place inside the string, it doesn't make a copy of the token. Passing a pointer to the space where you want the result to end up, and creating that space inside the function (with the correct size), then copying the result into it, would be the right thing to do. All this is quite subtle. Let me know if this is not clear!

这篇关于如何提取在C串子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆