简体   繁体   English

使用位于C字符串中的两个char *获取子字符串

[英]Get a substring using two char* positionned in a C string

Let long_text , keyword1 and keyword2 be three char* pointers. long_textkeyword1keyword2为三个char*指针。 _keyword1_ and _keyword2_ being two substrings of long_text . _keyword1__keyword2_是两子long_text Using strstr(long_text, keyword1) I can get a char* which points to the first occurrence of keyword1 in long_text , and using strstr(long_text, keyword2) I can get a char* which points to the first occurrence of keyword2 in long_text . 使用strstr(long_text, keyword1)我可以得到一个char* ,它指向long_textkeyword1的第一个匹配long_text ,而使用strstr(long_text, keyword2)我可以得到一个char* ,其指向long_textkeyword2的第一个匹配long_text keyword1 and keyword2 do not overlap. keyword1keyword2不重叠。

Is there a way to extract a substring from long_text representing the string between keyword1 and keyword2 using the two char* obtained from strstr() ? 有没有一种方法来提取一个子long_text代表之间的串keyword1keyword2使用两个char*从获得strstr()

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

int main(){
    char* long_text = "this is keyword1 and this is keyword2 in long_text";
    char* keyword1 = "keyword1";
    char* keyword2 = "keyword2";

    char* k1_start = strstr(long_text, keyword1);
    char* k2_start = strstr(long_text, keyword2);

    // TODO Be able to print " and this is "

    return 0;
}

This is the part you are missing 这是你所缺少的部分

// Move k1_start to end of keyword1
k1_start += strlen(keyword1);

// Copy text from k1_start to k2_start
char sub_string[32];
int  len = k2_start - k1_start;

strncpy(sub_string, k1_start, len);

// Be sure to terminate the string
sub_string[len] = '\0';

Yeah.. 是啊..

This is C-like and uses char * and supporting char array. 这类似于C,并使用char *和支持char数组。

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

int main(void) {
    char* long_text = "key1(text)key2";
    char* keyword1 = "key1";
    char* keyword2 = "key2";

    char* k1 = strstr(long_text, keyword1);
    char* k2 = strstr(long_text, keyword2);

    // from first char of match up to first char of second match
    char text[strlen(k1) - strlen(k2)];

    int len = (int)strlen(k1);
    for (int i = 0;; i++) {
        text[i] = *k1; k1++;
        if (i == (len - strlen(k2))) {
            text[len - strlen(k2)] = '\0';
            break;
        }
    }
    char* res;
    //We have now only keyword1 + middle part, compare until diff.,
    //then remember position and just iterate from to it later.
    int j = 0;
    for (int i = 0;; i++) {
        if (*keyword1 == text[i]) {
            j = i;
            keyword1++;
        } else {
            res = &text[++j];
            break;
        }
    }

    printf("%s\n", res);

    return 0;
}
void look_for_middle(const char *haystack,
                     const char *needle1, const char *needle2) {
    const char *start; /* start of region between keywords */
    const char *end;   /* end of region between keywords */
    const char *pos1;  /* match needle1 within haystack */
    const char *pos2;  /* match needle2 within haystack */
    int length;  /* length of region between needles */

    /* Look for needles in haystack. */
    pos1 = strstr(haystack, needle1);
    pos2 = strstr(haystack, needle2);
    if (pos1 != NULL && pos2 != NULL) {
        /* Both needles were found. */
        if (pos1 < pos2) {
            /* needle1 appears before needle2 */
            start = pos1 + strlen(needle1);
            end = pos2;
        } else {
            /* needle2 appears before needle1 */
            start = pos2 + strlen(needle2);
            end = pos1;
        }
        length = end - start;
    } else {
        /* One or more needles were not found. */
        start = NULL;
        end = NULL;
        length = 0;
    }

    /* Report result. */
    if (start != NULL) {
        /* Both needles were found. */
        if (length < 0) {
            printf("Needles overlap\n");
        } else {
            /*
             * In this printf, the "precision" of the string
             * is set so that it only prints the portion between
             * the needles.
             */
            printf("Middle pos %d len %d: %.*s\n",
                   (int)(start - haystack), length, length, start);
        }
    } else {
        if (pos1 == NULL) {
            printf("Needle 1 not found\n");
        }
        if (pos2 == NULL) {
            printf("Needle 2 not found\n");
        }
    }
}

This function can do your job . 此功能可以完成您的工作。 .

   char* strInner(char *long_text , char *keyword1 , char* keyword2)
    {

        char * a = strstr(long_text,keyword1);
        a+= strlen(keyword1);

        if(a==NULL)
        {
            cout<<"Keyword1 didn't found ! ";
            return a ;
        }

        char * b = strstr(a,keyword2);

        if(b==NULL)
        {
            cout<<"Keyword2 didn't found Or, found before Keyword1 ! ";
            return b ;
        }

        char *inner = (char*)malloc(strlen(a)-strlen(b));
        memcpy(inner,a,strlen(a)-strlen(b) );

        return inner ;

    }

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

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