c中的字符串解析和子字符串

时间:2010-04-25 15:19:19

标签: c string parsing

我正在尝试以良好的方式解析下面的字符串,以便我可以获得子字符串stringI-wantToGet

const char *str = "Hello \"FOO stringI-wantToGet BAR some other extra text";

str的长度会有所不同,但总是相同的模式 - FOO和BAR

我想到的是:

const char *str = "Hello \"FOO stringI-wantToGet BAR some other extra text";

char *probe, *pointer;
probe = str;
while(probe != '\n'){
    if(probe = strstr(probe, "\"FOO")!=NULL) probe++;
    else probe = "";
    // Nulterm part
    if(pointer = strchr(probe, ' ')!=NULL) pointer = '\0';  
    // not sure here, I was planning to separate it with \0's
}

任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:5)

我手上有一些时间,所以你就是。

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

int getStringBetweenDelimiters(const char* string, const char* leftDelimiter, const char* rightDelimiter, char** out)
{
    // find the left delimiter and use it as the beginning of the substring
    const char* beginning = strstr(string, leftDelimiter);
    if(beginning == NULL)
        return 1; // left delimiter not found

    // find the right delimiter
    const char* end = strstr(string, rightDelimiter);
    if(end == NULL)
        return 2; // right delimiter not found

    // offset the beginning by the length of the left delimiter, so beginning points _after_ the left delimiter
    beginning += strlen(leftDelimiter);

    // get the length of the substring
    ptrdiff_t segmentLength = end - beginning;

    // allocate memory and copy the substring there
    *out = malloc(segmentLength + 1);
    strncpy(*out, beginning, segmentLength);
    (*out)[segmentLength] = 0;
    return 0; // success!
}

int main()
{
    char* output;
    if(getStringBetweenDelimiters("foo FOO bar baz quaz I want this string BAR baz", "FOO", "BAR", &output) == 0)
    {
        printf("'%s' was between 'FOO' and 'BAR'\n", output);
        // Don't forget to free() 'out'!
        free(output);
    }
}

答案 1 :(得分:0)

在第一个循环中,扫描直到找到第一个分隔符字符串。在那里设置锚点指针。

如果找到,从锚点ptr,在第二个循环中,扫描,直到找到你的第二个分隔符字符串或你遇到字符串的结尾

如果不在字符串的末尾,则复制锚点ptr和第二个ptr之间的字符(加上你需要的空格等调整)