Get a value between two delimiters

Hi guys,

I'm using the Ethercard library and I'm getting a tcp response from a web api. I want to extract the data from the web response. I've managed to put the data between two "," so basically my tcp response looks like this when I prinln it :

HEADER
XXXXX
XXXXX
XXXXXXX

,datahere,

Here I only want to get 'datahere', as an int because it is a 0 or a 1 my web api answers.

The simplified code is basically this :

String reply = ether.tcpReply(session);

int commaIndex = reply.indexOf(',');

int secondCommaIndex = reply.indexOf(',', commaIndex + 1);

String access = reply.substring(commaIndex + 1, secondCommaIndex);

int g = access.toInt();

if(g == 1){
Serial.println("Access granted");
}

However it doesn't seem to be working, any idea ?

Thanks :slight_smile:

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

The parse example in Serial Input Basics may give you ideas for how to extract your data.

...R

Would it be possible for you to give us some clues as to what it does rather than just saying "it doesn't seem to be working"

One piece of advice would be to use strings instead of Strings and use the strtok() function to split up the string.

It would also help if you posted your whole program

If you want a little practice using pointers and not using strtok(), try this:

char *prompt[] = {"Enter the string to pick apart: ", "Starting delimiter:", "Ending delimiter"};
int which = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char buffer[50];
  char result[50];
  char delimiterString[3];      // Only start and end delimiter characters
  char c, delimiterStart, delimiterEnd;
  int index;
  static int flag = true;

  if (flag == true) {
    Serial.println(prompt[which]);
    flag = false;
  }
  if (Serial.available() > 0) {       // Test data: "12#345678&90" 
    index = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    switch (which) {
      case 0:
        buffer[index] = NULL;
        strcpy(result, buffer);           // Save it
        Serial.print("Input string: ");
        Serial.println(result);
        which++;                          // Next input
        flag = true;                      // Show prompt
        break;
        
      case 1:
        buffer[index] = NULL;
        strcpy(delimiterString, buffer);  // Save it
        Serial.print("Starting delimiter: ");
        Serial.println(delimiterString[0]);
        which++;                          // Next input
        flag = true;                      // Show prompt
        break;
        
      case 2:
        buffer[index] = NULL;
        strcat(delimiterString, buffer);  // Save it
        Serial.print("Ending delimiter: ");
        Serial.println(delimiterString[1]);
        
        PullApart(delimiterString, result);
        
        Serial.print("Substring = ");
        Serial.println(result);
        which = 0;                        // Start over
        flag = true;                      // Show prompt
        break;
        
    }
  }
}

void PullApart(char *delimiters, char *input)
{
  char *ptr;
  int indexes[2] = {-1, -1};

  ptr = input;
  while (*ptr) {
    if (*ptr == delimiters[0]) {        // Is this the starting delimiter?
      indexes[0] = ptr - input + 1;     // Yep
    }
    if (*ptr == delimiters[1]) {        // Is this the ending delimiter?
      indexes[1] = ptr - input;         // Yep
      break;
    }
    ptr++;                              // Next character...
  }
  if (indexes[0] != 0 && indexes[1] != 0) {   // Did we find both delimiters?
    input[indexes[1]] = NULL;                 // Make it a string
    strcpy(input, &input[indexes[0]]);
  } else {
    input[0] = NULL;
  }
}

If you input the test string 12#345678&90 which uses # and & as the starting and ending delimiters, you can see how the substring 345678 is pulled out of the main string using pointers.

Thanks for all your replies guys. Actually, my code wasn't wrong but I was getting an error because I didn't see a delimiter , in the output, so basically I was getting the wrong part of the text and not 0 or 1. I'll test strok() though.

Ty again guys