Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match text in string in Arduino

Tags:

arduino

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

I have:

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

And I am trying to match:

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

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

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

Where is the error?

like image 691
Mindaugas Avatar asked Feb 17 '11 13:02

Mindaugas


People also ask

How do I find a word in a string Arduino?

In order to check if a specific substring exists within a string in Arduino, the indexOf() function can be used. This returns the index of the first occurrence of the character or a string that you are searching for within another string.

Can you use == for strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you match string values?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

Can you compare strings in Arduino?

compareTo() Compares two Strings, testing whether one comes before or after the other, or whether they're equal. The strings are compared character by character, using the ASCII values of the characters. That means, for example, that 'a' comes before 'b' but after 'A'.


4 Answers

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)

I'd also recommend visiting:

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

like image 191
fulvio Avatar answered Oct 03 '22 15:10

fulvio


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;
}
like image 38
Ydakilux Avatar answered Oct 03 '22 15:10

Ydakilux


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

This is how I'm doing it. Hope it helps.

like image 42
Georgian Avatar answered Oct 03 '22 15:10

Georgian


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

You can't put the string with quotes only you need to cast the variable :)

like image 34
ZiTAL Avatar answered Oct 03 '22 16:10

ZiTAL