Split string by a substring

前端 未结 4 604
清酒与你
清酒与你 2021-01-19 16:48

I have following string:

char str[] = \"A/USING=B)\";

I want to split to get separate A and B values with /

相关标签:
4条回答
  • As others have pointed out, you can use strstr from <string.h> to find the delimiter in your string. Then either copy the substrings or modify the input string to split it.

    Here's an implementation that returns the second part of a split string. If the string can't be split, it returns NULL and the original string is unchanged. If you need to split the string into more substrings, you can call the function on the tail repeatedly. The first part will be the input string, possibly shortened.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *split(char *str, const char *delim)
    {
        char *p = strstr(str, delim);
    
        if (p == NULL) return NULL;     // delimiter not found
    
        *p = '\0';                      // terminate string after head
        return p + strlen(delim);       // return tail substring
    }
    
    int main(void)
    {
        char str[] = "A/USING=B";
        char *tail;
    
        tail = split(str, "/USING=");
    
        if (tail) {
            printf("head: '%s'\n", str);
            printf("tail: '%s'\n", tail);
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-19 17:05

    I known strtok() but it just split by one character as delimiter

    Nopes, it's not.

    As per the man page for strtok(), (emphasis mine)

    char *strtok(char *str, const char *delim);

    [...] The delim argument specifies a set of bytes that delimit the tokens in the parsed string. [...] A sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter. [...]

    So, it need not be "one character" as you've mentioned. You can using a string, like in your case "/USING=" as the delimiter to get the job done.

    0 讨论(0)
  • 2021-01-19 17:10

    Here is a little function to do this. It works exactly like strtok_r except that the delimiter is taken as a delimiting string, not a list of delimiting characters.

    char *strtokstr_r(char *s, char *delim, char **save_ptr)
    {
        char *end;
        if (s == NULL)
            s = *save_ptr;
    
        if (s == NULL || *s == '\0')
        {
            *save_ptr = s;
            return NULL;
        }
    
        // Skip leading delimiters.
        while (strstr(s,delim)==s) s+=strlen(delim);
        if (*s == '\0')
        {
            *save_ptr = s;
            return NULL;
        }
    
        // Find the end of the token.
        end = strstr (s, delim);
        if (end == NULL)
        {
            *save_ptr = s + strlen(s);
            return s;
        }
    
        // Terminate the token and make *SAVE_PTR point past it.
        memset(end, 0, strlen(delim));
        *save_ptr = end + strlen(delim);
        return s;
    }
    
    0 讨论(0)
  • 2021-01-19 17:14

    See this. I got this when I searched for your question on google.

    In your case it will be:

    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char* argv [])
    {
        char theString [16] = "abcd/USING=efgh";
        char theCopy [16];
        char *token;
        strcpy (theCopy, theString);
        token = strtok (theCopy, "/USING=");
        while (token)
        {
            printf ("%s\n", token);
            token = strtok (NULL, "/USING=");
        }
    
        return 0;
    }
    

    This uses /USING= as the delimiter.

    The output of this was:

    abcd                                                                                                                                                                                                                      
    efgh 
    

    If you want to check, you can compile and run it online over here.

    0 讨论(0)
提交回复
热议问题