【问题标题】:Arduino - Comparing a split string with another string [closed]Arduino - 将拆分字符串与另一个字符串进行比较[关闭]
【发布时间】:2012-11-14 22:46:11
【问题描述】:

我正在尝试将爆炸的字符串与 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\0,但它似乎不包含任何这些字符,因为当我检查“$test\r " 或其他它仍然不返回 true。

【问题讨论】:

    标签: c++ split serial-port arduino string-comparison


    【解决方案1】:

    在这一行:

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

    使用 strcmp 代替 == 比较。

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多