Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

c - Removing substring from a 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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...