Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...