2011-01-28 30 views
10

私は 'バッファ'と呼ばれる文字列を受け取り、それを解析するC関数を持っています。これはキーワードと一致し、それを使って構造体に値を代入します。文字列から部分文字列を削除しますか?

しかし、一部のキーワードは完全に無視したいと思います。

このプログラムは、VCardファイル(.vcf、仮想名刺)を解析します。

FN;CHARSET=UTF-8:David Celery 

FNが興味キーワードイムで、デビッド・セロリは、FNに関連付けられた値である。ここでは

は、サンプルラインバッファが提供する可能性があります。

しかし、CHARSET = UTF-8は気にしません。

私のバッファをスキャンし、単純に "CHARSET = UTF-8"を ""に置き換える方法があるので、それを解析することを心配する必要はありません私は)無視したい

おかげで、他の

+1

これまでに何を試みましたか? C言語で文字列を使った作業をしていて、この基本的なことができない場合は、文字列をサポートする言語(C++、Java、C#、Python、Delphiなど)を使用することを検討することをお勧めします。 –

答えて

11

のようなシンプルなANSI C溶液を見てみます:代わりにそれらを除去する

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

このコードを使用しようとすると警告とセグメンテーションが表示されます。どこかで間違いはありますか?私はc99を使用しています。 – Blackbinary

+8

'strlen(toremove)'を格納した方が効率が良いでしょう。ループごとに2回は決める必要はありません。 – ThiefMaster

+0

パフォーマンスは問題ではありませんでした。このコードは、より多くの出現のためにもうまく動作します。私はあなたのコンパイラの問題だと思っています。 – user411313

2

誰かがC文字列が見つけ、あなたが役に立つ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

私は世界のすべての問題を解決しようとしているとは思っていませんでしたが、アプローチについての提案はしていますが、YMMV。 – jlehr

関連する問題