Split string by delimiter or check string for a value.

Hi there,

I'm having this problem: I send a string "message" with the values "1234:12345e1234:0:100" Now i've tried different manners to split the string and compare it which ofcourse didn't work. All the help is welcome :) thanks in advance!

regards,

have a look at strtok()

You can also have a look at the default c-string functions that are part of the standard libraries.

J-M-L: have a look at strtok()

You can also have a look at the default c-string functions that are part of the standard libraries.

Thankyou i'll look into it!

Regards,

I get this error: "void value not ignored as it ought to be"

When i try to execute this statement:

String checkMessage(String message)
{
  int str_len = message.length() + 1;
  char char_array[str_len];
  char *p = message.toCharArray(char_array, str_len);
  char *str;
  while ((str = strtok_r(p, ":", &p)) != NULL)
    Serial.println(str);
}

if you are using the String class (I don't like it on small memory devices) just use the methods provided with that class such as indexOf() and lastIndexOf() Methods

Who mentioned using the strtok_r() function? The recommendation was to use strtok() which is far simpler.

I get this error: "void value not ignored as it ought to be"

The error message includes the exact line number where the problem occurs (at least where the compiler fell on its lips). Of course, the snippet you posted doesn't include line numbers, so, you are the only one that can solve the problem.

There really is no excuse for using Strings for any purpose.

Your function promises to return a String, but you don't. Liar.

I used the below code to split some strings.

void split(char* data, char* delim, int index, char* out) {
  char *copy = (char*)malloc(strlen(data) + 1);
  strcpy(copy, data);
  char *str;
  int i = 0;

  while ((str = strtok_r(copy, "|", ©)) != NULL){ // delimiter is the semicolon
    if (i == index)
      break;
    i++;
  }
  free(copy);
  strcpy(out,str);
}

Note: I accidentally ignored delim variable and hard coded the delimiter as "|", you might try changing the hardcoded delimiter with delim variable.

say my string is like "MTR|2|10|0"

I call the function like

char receivedChars[30]; //this variable holds the value "MTR|2|10|0"
char splitted[5];

//code to populate the value to receivedChars.

split(receivedChars,"|",0,splitted);
//splitted will have value "MTR"
split(receivedChars,"|",1,splitted);
//splitted will have value 2

Hope the code might help you.

J-M-L: if you are using the String class (I don't like it on small memory devices) just use the methods provided with that class such as indexOf() and lastIndexOf() Methods

Will look into it thanks! :)

PaulS: Who mentioned using the strtok_r() function? The recommendation was to use strtok() which is far simpler. The error message includes the exact line number where the problem occurs (at least where the compiler fell on its lips). Of course, the snippet you posted doesn't include line numbers, so, you are the only one that can solve the problem.

There really is no excuse for using Strings for any purpose.

Your function promises to return a String, but you don't. Liar.

The liar part really got to me :(

sarouje: I used the below code to split some strings.

void split(char* data, char* delim, int index, char* out) {
  char *copy = (char*)malloc(strlen(data) + 1);
  strcpy(copy, data);
  char *str;
  int i = 0;

  while ((str = strtok_r(copy, "|", ©)) != NULL){ // delimiter is the semicolon     if (i == index)       break;     i++;   }   free(copy);   strcpy(out,str); }




*Note: I accidentally ignored delim variable and hard coded the delimiter as "|", you might try changing the hardcoded delimiter with delim variable.*

say my string is like "MTR|2|10|0"

I call the function like



char receivedChars[30]; //this variable holds the value "MTR|2|10|0" char splitted[5];

//code to populate the value to receivedChars.

split(receivedChars,"|",0,splitted); //splitted will have value "MTR" split(receivedChars,"|",1,splitted); //splitted will have value 2




Hope the code might help you.

Thank you very much i'll try it!

regards :)

I too was not happy with the use of the work 'Liar' but why are you not using the built in function strtok() which has been around for many years and is well tested. You are just adding to your test requirment and to your maintenance task without any gain.

The liar part really got to me

Well, add a return statement, so that you are not lying to the compiler. Or, change the return type to void if you really don't have anything to return.