How to save in variables a split string by delimiters

Hi, I have a comma delimited string that I need to separate it in substring and save it in variables of a structure, to separate it I am using strtok, and I can print it through the serial port, but how do I save it in the variables?

Here is mi code:

char Mystring[50] = "Hello,World!";
char * token;

struct words {
  String one;
  String two;
};

void setup() {
  struct words Mywords;
  Serial.begin(115200);
  Serial.println();
  token = strtok(Mystring, ",");
  while ( token != NULL ) {
    Serial.println( token ); //printing each token
    token = strtok(NULL, ",");

    Mywords.one = ???
    Mywords.two = ???
  }
}

void loop() {
}

Try this:

char Mystring[50] = "Hello,World!";
char * token;
char * strings[3];  // pointers to strings to receive the parsed data

struct words
{
   char one[8];  // sorry, I don't use Strings
   char two[8];
};

void setup()
{
   struct words Mywords;
   Serial.begin(115200);
   Serial.println();
   byte index = 0;  // index to index the strings array
   token = strtok(Mystring, ",");  // get the first part
   while (token != NULL )
   {
      strings[index] = token;  // put the parsed part into the array
      index++;                 // increment the array position
      Serial.println( token ); //printing each token
      token = strtok(NULL, ",");  // get the next part
   }

   strcpy(Mywords.one, strings[0]);
   strcpy(Mywords.two, strings[1]);
   Serial.print("Mywords.one = ");
   Serial.print(Mywords.one);
   Serial.print("   Mywords.two = ");
   Serial.println(Mywords.two);
}

void loop()
{
}

I added an array of pointers to receive the parsed parts and an index to increment the array position as the parts are parsed.

I changed the struct to strings because I do not like using String and frankly don't know much about handling them.

If you are willing to settle for an array of C-strings instead, this works:

char Mystring[50] = "Hello,World!";
char * token;
char strings[2][10] = {0}; //two strings to receive the parsed data (each 9 characters max)

void setup()
{
  Serial.begin(9600);
  Serial.println();
  byte index = 0;  // index to index the strings array
  token = strtok(Mystring, ",");  // get the first part
  while (token != NULL )
  {
    strcpy(strings[index], token);  //safer: use strncpy() instead
    Serial.println( strings[index] ); //printing each substring
    index++;                 // increment the array position
    token = strtok(NULL, ",");  // get the next part
  }
}

void loop()
{
}

Just posting some code for an increase in post count.

void fParseLIDAR_ReceivedSerial ( void * parameters )
{
  // distribute received LIDAR info
  String sTmp = "";
  sTmp.reserve ( 20 );
  String sMessage = "";
  sMessage.reserve ( StringBufferSize300 );
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
    xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
    //        Serial.println ( sMessage );
    int commaIndex = sMessage.indexOf(',');
    sTmp.concat ( sMessage.substring(0, commaIndex) );
    sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
    //    Serial.println ( sTmp );
    if ( sTmp == "!" )
    {
      xSemaphoreGive ( sema_LIDAR_OK );
      //  Display info from LIDAR
      sLIDAR_Display_Info = sMessage;
    }
    if ( sTmp == "$" )
    {
      xEventGroupSetBits( eg1, evtResetWatchDogVariables );
    }
    if ( sTmp == "#")
    {
      // Serial.println ( "#" );
      xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
      sLIDAR_Alarm_info = sMessage;
      xSemaphoreGive( sema_LIDAR_Alarm );
      xEventGroupSetBits( eg, evtfLIDAR_Alarm );
    }
    //    Serial.println ( "parse serial ok" );
    sTmp = "";
    sMessage = "";
    xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
    //    Serial.print( "fParseReceivedSerial " );
    //    Serial.print(uxTaskGetStackHighWaterMark( NULL ));
    //    Serial.println();
    //    Serial.flush();
  }
  vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )
/////////////////////////////////////////////////////////////////////////////////////////////////////
void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

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