Substring of a char array (grab everything after a delimiter chain)

I have this char array :

char webPacket[1024];

It gets populated after I send a request to my api :

  // ...
  while (client.connected() && ((millis() - startTime) < apiResponseTimeout)) {
    if (client.available()) {
      c = client.read();

      webPacket[i] = c;
      i++;
    }

    //yield();
  }
  webPacket[i] = '\0';
  // ...

This is what webPacket contains after the api call :

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 27 Aug 2022 19:20:22 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 18
Connection: keep-alive
X-Redacted-For-Privacy: secret

+MP:127.0.0.1

I want to put everything AFTER +MP: inside a new char array.
This is what I tried :

  char tmp[1024];
  strcpy(tmp, webPacket);
  char *strTokIndex = strtok(tmp, "+MP:");
  strTokIndex = strtok(NULL, "+MP:");
  char res[256] = {0};
  strcpy(res, strTokIndex);

I expect res to have value 127.0.0.1.
This does not work and just crashes/resets my ESP.

How can I grab everything after (and excluding) "+MP:" and put it in a new char array?

(I do not want to use the String library)

The strstr() function will return a pointer to the position of one string within another

1 Like

How do I tell it to copy until the end of the data? There are multiple CR LF in webPacket so I cannot use those.

memcpy(res, (strstr(webPacket, "+MP:") + 4), ???);
char* string1 = { "HTTP/1.1 200 OK\nServer: nginx\nDate: Sat, 27 Aug 2022 19:20:22 GMT\nContent-Type: text/html; charset=UTF-8\nContent-Length: 18\nConnection: keep-alive\nX-Redacted-For-Privacy: secret\n\n+MP:127.0.0.1" };

void setup()
{
  Serial.begin(115200);
  char * position = strstr(string1, "+MP:");
  Serial.println(position+4);
}

void loop()
{
}

Serial.println() outputs the content of a var in the serial monitor.
It does not put a substring of a char[] inside an other char[].
You can learn more about Arduino serial basics here : Serial - Arduino Reference

:slight_smile:

"CR LF" has nothing to do with the "end of string" and do not prevent you from using string functions.
String in C is terminated by '\0' and not by line feed

It seems to me that you don't understand the answer #4

yes you are right, I would just need to look for the null terminating char, not sure why CRLF came to my mind.

answer #4 is unrelated (or at least too late). See #3.

It seems to you because you did not understand it.
Please read it again. Since you get position of your substring in the main buffer(as said in #4) - you can easily copy it to another buffer with standard string copy function.

there is nothing to see, there is nonsense about CR LF

#4 lacked that information.
I would not post here if it was obvious to me that you can just copy until \0 just having the start pointer.
For googlers, here is how I solved it:

  char * ptr = strstr(webPacket, "+MP:");
  char res[256];
  strcpy(res, ptr + 4);

Thank you for your help

Hurray !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.