删除 C 字符串的第一部分

发布于 2024-10-07 00:19:37 字数 323 浏览 3 评论 0原文

我很难弄清楚这一点。我有一个 C 字符串,我想删除它的第一部分。比方说:“食物、数量、卡路里”。我想复制这些值中的每一个,但不是逗号。我找到逗号,并将逗号的位置返回到我的方法。然后我使用

strncpy(aLine.field[i], theLine, end);

将“theLine”复制到位置“i”处的数组,仅包含第一个“end”字符(第一次,“end”将为4,因为这是第一个逗号所在的位置)。但是,因为它在循环中,所以我想从数组中删除“Food”,然后再次执行该过程。但是,我不知道如何删除第一部分(或向前移动数组指针?)并保留其余部分。任何帮助都会有用的!

I'm having a lot of trouble figuring this out. I have a C string, and I want to remove the first part of it. Let's say its: "Food,Amount,Calories". I want to copy out each one of those values, but not the commas. I find the comma, and return the position of the comma to my method. Then I use

strncpy(aLine.field[i], theLine, end);

To copy "theLine" to my array at position "i", with only the first "end" characters (for the first time, "end" would be 4, because that is where the first comma is). But then, because it's in a Loop, I want to remove "Food," from the array, and do the process over again. However, I cannot see how I can remove the first part (or move the array pointer forward?) and keep the rest of it. Any help would be useful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

与风相奔跑 2024-10-14 00:19:37

您需要的是用逗号作为分隔符来截断字符串。

你需要 strtok 来做到这一点。这是一个示例代码:

int main (int argc, const char * argv[]) {


    char *s = "asdf,1234,qwer";
    char str[15];
    strcpy(str, s);
    printf("\nstr: %s", str);
    char *tok = strtok(str, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);

    return 0;
}

这将为您提供以下输出:

str: asdf,1234,qwer
tok: asdf
tok: 1234
tok: qwer

What you need is to chop off strings with comma as your delimiter.

You need strtok to do this. Here's an example code for you:

int main (int argc, const char * argv[]) {


    char *s = "asdf,1234,qwer";
    char str[15];
    strcpy(str, s);
    printf("\nstr: %s", str);
    char *tok = strtok(str, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);
    tok = strtok(NULL, ",");
    printf("\ntok: %s", tok);

    return 0;
}

This will give you the following output:

str: asdf,1234,qwer
tok: asdf
tok: 1234
tok: qwer
我为君王 2024-10-14 00:19:37

如果一定要保留原来的字符串,那么strtok。如果没有,可以将各个分隔符替换为'\0',直接使用获取到的字符串:

char s_RO[] = "abc,123,xxxx", *s = s_RO;
while (s){
    char* old_str = s;
    s = strchr(s, ',');
    if (s){
        *s = '\0';
        s++;
    };
    printf("found string %s\n", old_str);
};

If you have to keep the original string, then strtok. If not, you can replace each separator with '\0', and use the obtained strings directly:

char s_RO[] = "abc,123,xxxx", *s = s_RO;
while (s){
    char* old_str = s;
    s = strchr(s, ',');
    if (s){
        *s = '\0';
        s++;
    };
    printf("found string %s\n", old_str);
};
我一直都在从未离去 2024-10-14 00:19:37

您可能想要使用的函数是 strtok()

这是一个很好的示例 - http:// /www.cplusplus.com/reference/clibrary/cstring/strtok/

The function you might want to use is strtok()

Here is a nice example - http://www.cplusplus.com/reference/clibrary/cstring/strtok/

魔法少女 2024-10-14 00:19:37

就我个人而言,我会使用 strtok()。

我不建议从字符串中删除提取的标记。删除字符串的一部分需要复制剩余的字符,效率不是很高。

相反,您应该跟踪您的位置并将所需的部分复制到新字符串中。

但是,我还是会使用 strtok()。

Personally, I would use strtok().

I would not recommend removing extracted tokens from the string. Removing part of a string requires copying the remaining characters, which is not very efficient.

Instead, you should keep track of your positions and just copy the sections you want to the new string.

But, again, I would use strtok().

七秒鱼° 2024-10-14 00:19:37

如果您知道逗号在哪里,则可以从该点开始继续读取字符串。

例如

void readTheString(const char *theLine)
{
    const char *wordStart = theLine;
    const char *wordEnd = theLine;

    int i = 0;
    while (*wordStart) // while we haven't reached the null termination character
    {
        while (*wordEnd != ',')
            wordEnd++;
        // ... copy the substring ranging from wordStart to wordEnd
        wordStart = ++wordEnd; // start the next word
    }
}

或类似的东西。
空终止检查可能是错误的,除非字符串也以“,”结尾...但您明白了。

无论如何,使用 strtok 可能是一个更好的主意。

if you know where the comma is, you can just keep reading the string from that point on.

for example

void readTheString(const char *theLine)
{
    const char *wordStart = theLine;
    const char *wordEnd = theLine;

    int i = 0;
    while (*wordStart) // while we haven't reached the null termination character
    {
        while (*wordEnd != ',')
            wordEnd++;
        // ... copy the substring ranging from wordStart to wordEnd
        wordStart = ++wordEnd; // start the next word
    }
}

or something like that.
the null termination check is probably wrong, unless the string also ends with a ','... but you get the idea.

anyway, using strtok would probably be a better idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文