如何在Arduino中匹配字符串中的文本

问题描述:

我对 Arduino 有一些关于如何匹配文本的问题.

I have some issues with Arduino about how to match text.

我有:

String tmp = +CLIP: "+37011111111",145,"",,"",0

我正在尝试匹配:

if (tmp.startsWith("+CLIP:")) {
    mySerial.println("ATH0");
}

但这不起作用,我不知道为什么.

But this is not working, and I have no idea why.

我试过substring,但结果是一样的.我不知道如何使用它,否则什么也不会发生.

I tried substring, but the result is the same. I don't know how to use it or nothing happens.

错误在哪里?

bool Contains(String s, String search) {
    int max = s.length() - search.length();

    for (int i = 0; i <= max; i++) {
        if (s.substring(i) == search) return true; // or i
    }

    return false; //or -1
} 

否则你可以简单地做:

if (readString.indexOf("+CLIP:") >=0)

我还建议您访问:

https://www.arduino.cc/en/Reference/String