10 years, 9 months ago.

Remove first characters from a string

How do I remove characters from a string?

I want to remove the first two characters, something like this:

Remove characters from string


mystring[remove(mystring) 0,2];  //   Error: Expected a "]"


esp.GetIP(rcv);   // rcv is the container that has SR192.168.1.3OK
                  // only want the 192.168.1.3 part


rcv[strlen(rcv) - 4] = '\0';  // gets rid of the 'OK'           
rcv[0] = ' ';
rcv[1] = ' ';       // clears the SR, but still have the same length.


I'm not sure which functions can be used with the Mbed compiler. 'remove' does go blue so I'm thinking that is acceptable.

I did look here but not sure if these examples will work on Mbed, For instance 'erase' example.

http://www.mochima.com/tutorials/strings.html

Thank you

Are you using real strings, or character arrays?

posted by Erik - 25 Jan 2015

Updated the question, I suppose character array, but you know what this is for, the ESP adds characters we don't want. What I have above works but not right.

posted by Paul Staron 25 Jan 2015

2 Answers

10 years, 9 months ago.

So those functions only work on real strings, not character arrays. And real strings take way more flash, ram and processing power. If you are working on it with your k22f that won't be an issue, on a kl25 it doesn't make you happy.

In general the easiest way to remove the first is characters is simply by doing char* new_rcv = rcv + 2;

A char array string is nothing more than a pointer to an array, of chars (makes sense :P). So if you put the pointer a little bit further, it removes the first ones.

Accepted Answer
10 years, 9 months ago.

How about this? No error checking is done here, though.

for (int i = 0 ; rcv[i+2] != 0 ; i++) {
rcv[i] = rcv[i+2] ;
}
rcv[i] = 0 ;

moto