Arduino - 将拆分字符串与另一个字符串进行比较
Arduino - Comparing a split string with another string
我正在尝试将分解的字符串与Arduino中的另一个字符串进行比较,但它不起作用。拆分的字符串是从串行端口读取的。
首先,这些是我用来分解字符串的函数:
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;
}
我尝试将分解的字符串与另一个字符串进行比较的部分如下所示:
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.
}
当我在串行监视器中输入$test,other时,我希望打印出匹配项,但没有打印任何内容。如果我这样做Serial.println(exploded[0]);
它会按应有的方式输出$test。我做错了什么?
我已经尝试寻找不可打印的字符,例如 r
、n
和