mid, truncate,find nTH occurence

inStr...NO, thats VBA. .find - NOPE...that's CString class I dont have apparently, .Tokenize, that would be great, but all end in red text.

strstr comes to mind but it has one catch, it finds the FIRST, only the first occurence in a string that you can only seem to copy from the head-on. If I had access to something that worked like mid or would truncate the beginning of a string, that would be great. I know it would fragment memory, but it's a small routine.

I want to extract four digit numbers in a sequence delimited by newlines that come in packets of 4-24 bytes. I just did it this evening in 30 minutes on VBA, C++ is more, shall we say bare. See my issue? Know a fix? Thanks!


Been there, done that. strstr only finds the first match. I edited the post because I forgot about strstr at first. I need about 4-5 matches as it's a four digit code in a upto 24byte packet, delimited by /n s.

It' still strstr. Just restart after last find.

And I dislike changing the question after answers, good luck with your projekt.

So something like this? I don't see it working. Is it something funny like VBA DIR function, where it retains the last folder somewhere in memory...?

 ix = 0;

    while (ix < len) {
      int endPoint = strstr(incomingPacket,"\n");
      strncpy ( char * code, const char * pack,endPoint )
      //what now - need to loop and find next. Asking.
      ix = ix + strlen(code) + 1;
      switch (code) {
        case 7004: openWeb() //magnet break

I don't see it working.

That crap won't even compile. It would have taken you next to no time at all to learn that. Figuring out why, and writing a decent bit of test code would have taken longer, but that IS what you have to do.

If have a pointer, and increment it by 1, it points to the next character. If you look for a 12 character string, and get a non-NULL pointer, that means that the string existed. To find the next occurrence, obviously you'd add 12 to the pointer you got, to point to the 1st character after the string you just found, as the next point to start searching from.

So loop thru char* packet while i is 1 to len of packet.
char* code[ix]=char* packet*, i++, ix++, then on each /n char ix = 0.*
Like that?

You might be better off looking at strtok(), which handles the tokenizing (properly) for you.

I'm trying strtok now! Thanks. I had no idea it existed. The sketch is having compile problems unrelated to strtok() I posted in programming.