C - 分割字符串

发布于 2024-12-11 21:00:37 字数 308 浏览 0 评论 0原文

C 中是否有任何预定义函数可以分割给定定界符的字符串?假设我有一个字符串:

"Command:Context"

现在,我想将“Command”和“Context”存储到二维字符数组

char ch[2][10]; 

两个不同变量

char ch1[10], ch2[10];

或我尝试使用循环的 ,并且效果很好。我只是好奇是否已经存在这样的功能,我不想重新发明轮子。请提供一个清楚的例子,非常感谢!

Is there any pre-defined function in C that can split a string given a delimeter? Say I have a string:

"Command:Context"

Now, I want to store "Command" and "Context" to a two dimensional array of characters

char ch[2][10]; 

or to two different variables

char ch1[10], ch2[10];

I tried using a loop and it works fine. I'm just curious if there is such function that already exists, I don't want to reinvent the wheel. Please provide a clear example, thank you very much!

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

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

发布评论

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

评论(2

左岸枫 2024-12-18 21:00:37

您可以使用 strtok

< a href="http://codepad.org/Da9ixGLP" rel="nofollow">在线演示

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

int main ()
{
    char str[] ="Command:Context";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str,":");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, ":");
    }
    return 0;
}

输出:

Splitting string "Command:Context" into tokens:
Command
Context

You can use strtok

Online Demo:

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

int main ()
{
    char str[] ="Command:Context";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str,":");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, ":");
    }
    return 0;
}

Output:

Splitting string "Command:Context" into tokens:
Command
Context
故事灯 2024-12-18 21:00:37

您可以按照以下示例使用 strtok 标记字符串:

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

int main (void) {
    char instr[] = "Command:Context";
    char words[2][10];
    char *chptr;
    int idx = 0;

    chptr = strtok (instr, ":");
    while (chptr != NULL) {
        strcpy (words[idx++], chptr);
        chptr = strtok (NULL, ":");
    }

    printf ("Word1 = [%s]\n", words[0]);
    printf ("Word2 = [%s]\n", words[1]);

    return 0;
}

输出:

Word1 = [Command]
Word2 = [Context]

strtok 函数有一些您可能需要注意的小问题。首先,它修改字符串本身以编织其魔力,因此不适用于字符串文字(例如)。

You can tokenise a string with strtok as per the following sample:

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

int main (void) {
    char instr[] = "Command:Context";
    char words[2][10];
    char *chptr;
    int idx = 0;

    chptr = strtok (instr, ":");
    while (chptr != NULL) {
        strcpy (words[idx++], chptr);
        chptr = strtok (NULL, ":");
    }

    printf ("Word1 = [%s]\n", words[0]);
    printf ("Word2 = [%s]\n", words[1]);

    return 0;
}

Output:

Word1 = [Command]
Word2 = [Context]

The strtok function has some minor gotchas that you probably want to watch out for. Primarily, it modifies the string itself to weave its magic so won't work on string literals (for example).

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