Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing substring from a string?

Tags:

c

string

I have a C function, which takes a string called 'buffer', and parses it, it will match keywords and use that to assign values in a structure.

However, some keywords I want to completely ignore.

This program parses VCard files (.vcf, virtual business cards).

Here is a sample line buffer might supply:

FN;CHARSET=UTF-8:David Celery

FN is a keyword im interested in, and David Celery is the value associated with FN.

However, CHARSET=UTF-8 is something I dont care about at all.

So my question is, is there a way for me to scan my buffer and simply replace 'CHARSET=UTF-8" with "", so that I don't have to worry about parsing it (and other similar keywords I just want to ignore).

Thanks,

like image 840
Blackbinary Avatar asked Jan 28 '11 21:01

Blackbinary


2 Answers

have a look at a simple ANSI C solution like:

void removeSubstring(char *s,const char *toremove)
{
  while( s=strstr(s,toremove) )
    memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
}
like image 174
user411313 Avatar answered Oct 06 '22 12:10

user411313


Someone else has a C string find and replace function that you might find useful here.

edit: included code snippet from link below, as per comment request

#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"
*/

Hope this helps.

like image 23
evandrix Avatar answered Oct 06 '22 11:10

evandrix