shn79:
Hello,
String platters [] = {"34ABC01", "34ABC02", "34ABC03", "34ABC04", "34ABC05"};
I have an array of type string, and I want to do a search with a variable of type string with indexof or some other method in it, and how to code to be able to produce a search result,
I wouldn't use String at all. I would do something like this (demo code you can compile and try):
int main (void)
{
char buffer[128]; // just a buffer to print to
const char *to_find = "34ABC03"; // string we want to find (comes from somewhere else in real life)
const char *platters[] = { // strings to search through
"34ABC01",
"34ABC02",
"34ABC03",
"34ABC04",
"34ABC05",
};
int len = (sizeof (platters) / sizeof (*platters)); // how many elements in array
int x; // generic loop counter
init(); // arduino setup
Serial.begin (115200);
for (x = 0; x < len; x++) {
sprintf (buffer, "Looking for %s (at index %d)...", to_find, x);
Serial.print (buffer);
if (strcmp (to_find, platters[x]) == 0) { // this is the key: a match returns 0
sprintf (buffer, "Found %s in array at index %d!\r\n", platters[x], x);
Serial.print (buffer);
} else {
sprintf (buffer, "Fail. %s is not %s at index %d!\r\n", to_find, platters[x], x);
Serial.print (buffer);
}
}
while (1); // done, nuthin else to do but hang out
}
The program should return text on the serial port like this:
[b]Looking for 34ABC03 (at index 0)...Fail. 34ABC03 is not 34ABC01 at index 0!
Looking for 34ABC03 (at index 1)...Fail. 34ABC03 is not 34ABC02 at index 1!
Looking for 34ABC03 (at index 2)...Found 34ABC03 in array at index 2!
Looking for 34ABC03 (at index 3)...Fail. 34ABC03 is not 34ABC04 at index 3!
Looking for 34ABC03 (at index 4)...Fail. 34ABC03 is not 34ABC05 at index 4![/b]
Hope this helps.
(by the way, those "strings" look like hexadecimal numbers to me. IF they are, you can use "strtoul" instead to convert them to unsigned long integers and then simply compare them numerically rather than as strings).