2011-01-28 44 views
10

मेरे पास एक सी फ़ंक्शन है, जो 'बफर' नामक एक स्ट्रिंग लेता है, और इसे पार करता है, यह कीवर्ड से मेल खाता है और संरचना में मूल्यों को असाइन करने के लिए इसका उपयोग करता है।स्ट्रिंग से सबस्ट्रिंग को हटा रहा है?

हालांकि, कुछ खोजशब्दों मैं पूरी तरह से अनदेखा करना चाहते।

इस कार्यक्रम VCard फ़ाइलें (.vcf, आभासी व्यापार कार्ड) पार्स करता है।

यहां नमूने लाइन बफर आपूर्ति दे सकती हैं:

FN;CHARSET=UTF-8:David Celery 

एफ एन एक कीवर्ड im में रुचि रखता है, और डेविड अजवाइन एफ एन के साथ जुड़े मूल्य है।

हालांकि, charset = utf-8 कुछ मैं बिल्कुल के बारे में परवाह न है।

तो मेरा सवाल यह है कि, मेरे बफर को स्कैन करने और बस 'CHARSET = UTF-8 "को" "के साथ बदलने का कोई तरीका है, ताकि मुझे इसे पार्स करने के बारे में चिंता न करें (और अन्य समान कीवर्ड मैं बस की अनदेखी करने) चाहते

धन्यवाद,

+1

आपने अभी तक क्या प्रयास किया है? और यदि आप सी में तारों के साथ काम कर रहे हैं, और आप इस बुनियादी को कुछ करने में सक्षम नहीं हैं, तो मेरा सुझाव है कि आप स्ट्रिंग सपोर्ट (जैसे सी ++, जावा, सी #, पायथन, डेल्फी इत्यादि) में निर्मित भाषा का उपयोग करने पर विचार करें। –

उत्तर

11

की तरह एक साधारण एएनएसआई सी समाधान पर एक नजर है:

void removeSubstring(char *s,const char *toremove) 
{ 
    while(s=strstr(s,toremove)) 
    memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove))); 
} 
+0

मुझे इस कोड का उपयोग करने की चेतावनी और एक सेगफॉल्ट मिलता है। क्या कहीं गलती है? यदि यह मायने रखता है तो मैं सी 99 का उपयोग कर रहा हूं। – Blackbinary

+8

यदि आपने 'strlen (toremove) संग्रहित किया है तो यह अधिक कुशल होगा - प्रति लूप को दो बार निर्धारित करने की आवश्यकता नहीं है। – ThiefMaster

+0

प्रदर्शन सवाल नहीं था; यह कोड अधिक घटनाओं के लिए भी ठीक काम करता है, मुझे लगता है कि यह आपकी या आपकी कंपाइलर समस्या – user411313

2

किसी और एक सी स्ट्रिंग खोजने के लिए और समारोह है कि आप उपयोगी here मिल सकती है की जगह है

संपादित करें:।। शामिल नीचे दिए गए लिंक से कोड का टुकड़ा, के अनुसार टिप्पणी अनुरोध

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
/* 
* Description: 
* Find and replace text within a string. 
* 
* Parameters: 
* src (in) - pointer to source string 
* from (in) - pointer to search text 
* to (in) - pointer to replacement text 
* 
* Returns: 
* Returns a pointer to dynamically-allocated memory containing string 
* with occurences of the text pointed to by 'from' replaced by with the 
* text pointed to by 'to'. 
*/ 
char *replace(const char *src, const char *from, const char *to) 
{ 
    /* 
    * Find out the lengths of the source string, text to replace, and 
    * the replacement text. 
    */ 
    size_t size = strlen(src) + 1; 
    size_t fromlen = strlen(from); 
    size_t tolen = strlen(to); 
    /* 
    * Allocate the first chunk with enough for the original string. 
    */ 
    char *value = malloc(size); 
    /* 
    * We need to return 'value', so let's make a copy to mess around with. 
    */ 
    char *dst = value; 
    /* 
    * Before we begin, let's see if malloc was successful. 
    */ 
    if (value != NULL) 
    { 
     /* 
     * Loop until no matches are found. 
     */ 
     for (;;) 
     { 
     /* 
      * Try to find the search text. 
      */ 
     const char *match = strstr(src, from); 
     if (match != NULL) 
     { 
      /* 
      * Found search text at location 'match'. :) 
      * Find out how many characters to copy up to the 'match'. 
      */ 
      size_t count = match - src; 
      /* 
      * We are going to realloc, and for that we will need a 
      * temporary pointer for safe usage. 
      */ 
      char *temp; 
      /* 
      * Calculate the total size the string will be after the 
      * replacement is performed. 
      */ 
      size += tolen - fromlen; 
      /* 
      * Attempt to realloc memory for the new size. 
      */ 
      temp = realloc(value, size); 
      if (temp == NULL) 
      { 
       /* 
       * Attempt to realloc failed. Free the previously malloc'd 
       * memory and return with our tail between our legs. :(
       */ 
       free(value); 
       return NULL; 
      } 
      /* 
      * The call to realloc was successful. :) But we'll want to 
      * return 'value' eventually, so let's point it to the memory 
      * that we are now working with. And let's not forget to point 
      * to the right location in the destination as well. 
      */ 
      dst = temp + (dst - value); 
      value = temp; 
      /* 
      * Copy from the source to the point where we matched. Then 
      * move the source pointer ahead by the amount we copied. And 
      * move the destination pointer ahead by the same amount. 
      */ 
      memmove(dst, src, count); 
      src += count; 
      dst += count; 
      /* 
      * Now copy in the replacement text 'to' at the position of 
      * the match. Adjust the source pointer by the text we replaced. 
      * Adjust the destination pointer by the amount of replacement 
      * text. 
      */ 
      memmove(dst, to, tolen); 
      src += fromlen; 
      dst += tolen; 
     } 
     else /* No match found. */ 
     { 
      /* 
      * Copy any remaining part of the string. This includes the null 
      * termination character. 
      */ 
      strcpy(dst, src); 
      break; 
     } 
     } 
    } 
    return value; 
} 
void test(const char *source, const char *search, const char *repl) 
{ 
    char *after; 
    after = replace(source, search, repl); 
    printf("\nsearch = \"%s\", repl = \"%s\"\n", search, repl); 
    if (after != NULL) 
    { 
     printf("after = \"%s\"\n", after); 
     free(after); 
    } 
} 
int main(void) 
{ 
    const char before[] = "the rain in Spain falls mainly on the plain"; 
    printf("before = \"%s\"\n", before); 
    test(before, "the", "THEE"); 
    test(before, "the", "A"); 
    test(before, "cat", "DOG"); 
    test(before, "plain", "PLANE"); 
    test(before, "ain", "AINLY"); 
    return 0; 
} 
/* my output 
before = "the rain in Spain falls mainly on the plain" 
search = "the", repl = "THEE" 
after = "THEE rain in Spain falls mainly on THEE plain" 
search = "the", repl = "A" 
after = "A rain in Spain falls mainly on A plain" 
search = "cat", repl = "DOG" 
after = "the rain in Spain falls mainly on the plain" 
search = "plain", repl = "PLANE" 
after = "the rain in Spain falls mainly on the PLANE" 
search = "ain", repl = "AINLY" 
after = "the rAINLY in SpAINLY falls mAINLYly on the plAINLY" 
*/ 

आशा इस मदद करता है।

0

string.h में तरीकों पर एक नजर डालें।

उदाहरण: (और CodePad पर)

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

int main() 
{ 
    const char * source = "FN;CHARSET=UTF-8:David Celery"; 
    const char * newBegin = strrchr(source, ':'); 
    if (!newBegin) 
    { 
     puts("Error!"); 
     return -1; 
    } 
    newBegin++; 
    puts(newBegin); 
    return 0; 
} 
0
इसके बजाय उन्हें हटाने की

, तो आप सिर्फ उन्हें अनदेखा कर सकता है, जैसे उदाहरण के लिए यह:

#define KEY_TO_IGNORE "CHARSET=UTF-8" 

char key[80]; 
char value[80]; 
char *text = "FN;CHARSET=UTF-8:David Celery"; 

sscanf(text, "%2s;" KEY_TO_IGNORE ":%s", key, value); 

printf("key: %s, value: %s\n", key, value); 
+0

मुझे नहीं लगता था कि हम सभी दुनिया की समस्याओं को हल करने की कोशिश कर रहे थे, बस दृष्टिकोण के रूप में सुझाव दें, लेकिन वाईएमएमवी। – jlehr

संबंधित मुद्दे