10 years, 8 months ago.

Remove characters before character “+”

I need to remove all characters before a certain character in a buffer, "+" in my case.

I do not know the amount of preceding characters as this changes. So I can't use a static option for instance:

strcpy(resultbuf, buf + 8);

Is there a similar simple function to do this?

Many thanks.

Paul

I think I have a solution:

int x = strcspn (buf,"+");

strcpy(resultbuf, buf + x);

This works, would this be the best way to do this?

posted by Paul Staron 13 Feb 2015

1 Answer

10 years, 8 months ago.

Dear Paul-san,

I would recommend you to take care of a case there is no '+'.

For example, usually I would do something like (may be I'm old style...)

char *ptr = 0 ;

if (buf) {
    ptr = strchr(buf, '+') ;
}

if (ptr) {
   strcpy(resultbuf, ++ptr) ;
} else {
   strcpy(resultbuf, buf) ;
}

In case you want to have null string
when there is no '+' you code is better.

moto

Accepted Answer

Good idea, now added error detection, this works:

snip

int x = strcspn (buf,"+");
    if(x){
        strcpy(webdata, buf + x);weberror=0;
        
        // rest of code
        
         }
            else {strcpy(webdata, buf);weberror=1;}
    }    

posted by Paul Staron 15 Feb 2015