How to match text in string in Arduino

How to match text in string in Arduino

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
} 

Otherwise you could simply do:

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

Id also recommend visiting:

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

I modified the code from gotnull. Thanks to him to put me on the track.

I just limited the search string, otherwise the substring function was not returning always the correct answer (when substrign was not ending the string). Because substring search always to the end of the string.

int StringContains(String s, String search) {
    int max = s.length() - search.length();
    int lgsearch = search.length();

    for (int i = 0; i <= max; i++) {
        if (s.substring(i, i + lgsearch) == search) return i;
    }

 return -1;
}

How to match text in string in Arduino

//+CLIP: 43660417XXXX,145,,0,,0
if (strstr(command.c_str(), +CLIP:)) { //Someone is calling
    GSM.print(F(ATAnr));
    Number = command.substring(command.indexOf() + 1);
    Number = Number.substring(0, Number.indexOf());
    //Serial.println(Number);
} //End of if +CLIP:

This is how Im doing it. Hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *