Go Down

Topic: FINDing within strings (Read 1 time) previous topic - next topic


Sep 14, 2010, 10:48 am Last Edit: Sep 14, 2010, 10:50 am by AlphaBeta Reason: 1
You could roll your own, like this:
Code: [Select]
     Search haystack for the needle
bool has(char *haystack, char *needle)
     int compareOffset = 0;
     //iterate through the zero terminated string heystack
     while(*haystack) {
           if (*haystack == *needle) { //we might have found a match
                 compareOffset = 0; //start at the current location in haystack
                 //see if the string from that location matches the needle
                 while (haystack[compareOffset] == needle[compareOffset]) {
                       if (needle[compareOffset]=='\0') {
                             return true; //we have reached the end of needle and everything matched
           haystack++; //increment the location in the string (the pointer)
     return false; //no match was found

Use like this:
Code: [Select]
void setp() {
 if (has("test string","t s")) { Serial.println("'test string' contains 't s'"); }
 if (!has("test string","s t")) { Serial.println("'test string' does not contain 's t'"); }

Go Up