Arduino - 将拆分字符串与另一个字符串进行比较

Arduino - Comparing a split string with another string

本文关键字:字符串 比较 另一个 拆分 Arduino      更新时间:2023-10-16

我正在尝试将分解的字符串与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。我做错了什么?

我已经尝试寻找不可打印的字符,例如 rn ,但它似乎不包含其中任何一个,因为当我检查"$test\r"或其他字符时,它仍然没有返回 true。

在这一行中:

if ("$test" == exploded[0])

使用 strcmp 而不是通过 == 进行比较。