Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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
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
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