Use startWith in Particle.subscribe handler

Hi!

How can I use ‘startsWith’ in a subscribe handler? I would like to check if the data that is subscribed to starts with a certain string.

Here is my code example:

char userPin[] = "1234";

void setup() {
    Particle.subscribe("verification", verificationHandler);
}

void loop() {

}

void verificationHandler(const char *event, const char *data) {

    if (data.startsWith(userPin)) {
    // do something
    }
}

Since this is not a String object but an C string, I’d use strncpm() instead of startsWith().
Alternatively you could copy the C string into a String and then use startsWith() on the copy, but I’d advise against that.

2 Likes

Thanks a lot @ScruffR! Works like a charm.

Thanks for the help!