I’m trying to compare an exploded string to another string in Arduino, but it won’t work. The string that is split is read from the serial port.
First of all, these are the functions I use for exploding the string:
int count_delimiters(char str[], const char* delimiters) {
int i, j, result = 0;
for (i = 0; i < strlen(str); ++i) {
for (j = 0; j < strlen(delimiters); ++j) {
if (str[i] == delimiters[j]) {
++result;
}
}
}
return (result + 1);
}
char** split(char str[], const char* delimiters) {
int result_size = count_delimiters(str, delimiters);
int i = 0;
char* result[result_size];
char* pch = strtok(str, ",");
while (pch != NULL)
{
result[i] = pch;
pch = strtok(NULL, ",");
++i;
}
return result;
}
The part where I try to compare the exploded string to another string looks like this:
char input_array[input.length()];
input.toCharArray(input_array, (input.length() + 1));
exploded = split(input_array, ",");
if ("$test" == exploded[0]) {
Serial.println("match"); // This code is never reached.
}
When I enter $test,other in the serial monitor I expect a match to be printed out but nothing is printed. If I do Serial.println(exploded[0]); it outputs $test as it should. What am I doing wrong?
I’ve already tried to look for non-printable characters like \r, \n and \0, but it doesn’t seem to contain any of these, because when I check for “$test\r” or the others it still doesn’t return true.
In this line:
use strcmp instead of comparing by ==.