to use strstr() in the same way as find_text() i had to do the following
char Haystack[] = "this is a test";
char Needle[] = "test";
Serial.println(Haystack);
Serial.println(Needle);
char * Pointer;
Pointer = strstr(Haystack, Needle);
Serial.println(&Pointer[0] - &Haystack[0]); // subtract the starting pointer of Haystack from the pointer returned by strstr()
Serial.println(find_text(Needle, Haystack));
Hi guys! I know this post is a bit old, but since it helped me a lot when looking for a simple, clean way to find text in strings, I decided to put my 2 cents in.
In case the needle is bigger than the haystack, the for loop will never end, which is not what a for loop is meant for in the first place, so it must be placed in a conditional if() or while() (I like to code with those two mostly). For example, it would look like this:
int find_text(String needle, String haystack)
{
int foundpos = -1;
if (needle.length() <= haystack.length())
{
for (int i = 0; i <= haystack.length() - needle.length(); i++)
{
if (haystack.substring(i,needle.length()+i) == needle)
foundpos = i;
}
}
return foundpos;
}
EDIT: I decided to post this so this "bug" would be considered and put in the description of the Arduino Playground site. Or use my code. Either way, I think it must be considered and mentioned for posterity.