简体   繁体   English

C将char数组拆分为不同的变量

[英]C split a char array into different variables

In C how can I separate a char array by a delimiter? 在C中如何用分隔符分隔char数组? Or is it better to manipulate a string? 或者操纵字符串更好? What are some good C char manipulation functions? 有什么好的C char操作函数?

#include<string.h>
#include<stdio.h>
int main()
{
    char input[16] = "abc,d";
    char *p;
    p = strtok(input, ",");

    if(p)
    {
    printf("%s\n", p);
    }
    p = strtok(NULL, ",");

    if(p)
           printf("%s\n", p);
    return 0;
}

you can look this program .First you should use the strtok(input, ",").input is the string you want to spilt.Then you use the strtok(NULL, ","). 你可以查看这个程序。首先你应该使用strtok(输入,“,”)。输入是你想要spilt的字符串。然后你使用strtok(NULL,“,”)。 If the return value is true ,you can print the other group. 如果返回值为true,则可以打印另一个组。

Look at strtok() . 看看strtok() strtok() is not a re-entrant function. strtok()不是可重入的函数。

strtok_r() is the re-entrant version of strtok(). strtok_r()是strtok()的可重入版本。 Here's an example program from the manual: 以下是手册中的示例程序:

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

   int main(int argc, char *argv[])
   {
       char *str1, *str2, *token, *subtoken;
       char *saveptr1, *saveptr2;
       int j;

       if (argc != 4) {
           fprintf(stderr, "Usage: %s string delim subdelim\n",argv[0]);
           exit(EXIT_FAILURE);
       }

       for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
           token = strtok_r(str1, argv[2], &saveptr1);
           if (token == NULL)
               break;
           printf("%d: %s\n", j, token);

           for (str2 = token; ; str2 = NULL) {
               subtoken = strtok_r(str2, argv[3], &saveptr2);
               if (subtoken == NULL)
                   break;
               printf(" --> %s\n", subtoken);
           }
       }

       exit(EXIT_SUCCESS);
   }

Sample run which operates on subtokens which was obtained from the previous token based on a different delimiter: 对基于不同分隔符从前一个令牌获得的子字符进行操作的样本运行:

$ ./a.out hello:word:bye=abc:def:ghi = :

1: hello:word:bye
 --> hello
 --> word
 --> bye
2: abc:def:ghi
 --> abc
 --> def
 --> ghi

One option is strtok 一个选择是strtok

example: 例:

char name[20];
//pretend name is set to the value "My name"

You want to split it at the space between the two words 你想把它分成两个单词之间的空格

split=strtok(name," ");

while(split != NULL)
{
    word=split;
    split=strtok(NULL," ");
}

You could simply replace the separator characters by NULL characters, and store the address after the newly created NULL character in a new char* pointer: 您可以简单地用NULL字符替换分隔符,并将新创建的NULL字符后的地址存储在新的char *指针中:

char* input = "asdf|qwer"
char* parts[10];
int partcount = 0;

parts[partcount++] = input;

char* ptr = input;
while(*ptr) { //check if the string is over
    if(*ptr == '|') {
        *ptr = 0;
        parts[partcount++] = ptr + 1;
    }
    ptr++;
}

Note that this code will of course not work if the input string contains more than 9 separator characters. 请注意,如果输入字符串包含超过9个分隔符,则此代码当然不起作用。

I came up with this.This seems to work best for me.It converts a string of number and splits it into array of integer: 我想出了这个。这似乎最适合我。它转换一个数字串并将其拆分为整数数组:

void splitInput(int arr[], int sizeArr, char num[])
{
    for(int i = 0; i < sizeArr; i++)
        // We are subtracting 48 because the numbers in ASCII starts at 48.
        arr[i] = (int)num[i] - 48;
}

This is how I do it. 我就是这样做的。

void SplitBufferToArray(char *buffer, char * delim, char ** Output) {

    int partcount = 0;
    Output[partcount++] = buffer;

    char* ptr = buffer;
    while (ptr != 0) { //check if the string is over
        ptr = strstr(ptr, delim);
        if (ptr != NULL) {
            *ptr = 0;
            Output[partcount++] = ptr + strlen(delim);
            ptr = ptr + strlen(delim);
        }

    }
    Output[partcount++] = NULL;
}

In addition, you can use sscanf for some very simple scenarios, for example when you know exactly how many parts the string has and what it consists of. 此外,您可以将sscanf用于一些非常简单的场景,例如,当您确切知道字符串有多少部分以及它包含的内容时。 You can also parse the arguments on the fly. 您还可以动态解析参数。 Do not use it for user inputs because the function will not report conversion errors. 不要将其用于用户输入,因为该功能不会报告转换错误。

Example: 例:

char text[] = "1:22:300:4444:-5";
int i1, i2, i3, i4, i5;
sscanf(text, "%d:%d:%d:%d:%d", &i1, &i2, &i3, &i4, &i5);
printf("%d, %d, %d, %d, %d", i1, i2, i3, i4, i5);

Output: 输出:

1, 22, 300, 4444, -5 1,22,300,4444,-5

For anything more advanced, strtok() and strtok_r() are your best options, as mentioned in other answers. 对于任何更高级的东西, strtok()strtok_r()是你最好的选择,如其他答案所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM