come on...
consider this code
String message = "/y?parm1=98";
void setup() {
Serial.begin(115200);
int positionSingleQuote = message.indexOf('parm1=');
int positionDoubleQuote = message.indexOf("parm1=");
Serial.println(positionSingleQuote);
Serial.println(positionDoubleQuote);
Serial.println((char) 'parm1=');
}
void loop() {}
when you try to compile you get:
[color=orange]/var/folders/xk/mwl4yt3j5rsgdmgmgjlqsk7w0000gn/T/arduino_modified_sketch_875666/sketch_nov17a.ino:5:45: warning: [b][color=red]character constant too long for its type[/color][/b]
int positionSingleQuote = message.indexOf('parm1=');
^
/var/folders/xk/mwl4yt3j5rsgdmgmgjlqsk7w0000gn/T/arduino_modified_sketch_875666/sketch_nov17a.ino:10:25: warning: [color=red][b]character constant too long for its type[/b][/color]
Serial.println((char) 'parm1=');
^
/var/folders/xk/mwl4yt3j5rsgdmgmgjlqsk7w0000gn/T/arduino_modified_sketch_875666/sketch_nov17a.ino: In function 'void setup()':
/var/folders/xk/mwl4yt3j5rsgdmgmgjlqsk7w0000gn/T/arduino_modified_sketch_875666/sketch_nov17a.ino:5:53: [color=red][b]warning: overflow in implicit constant conversion[/b][/color] [-Woverflow]
int positionSingleQuote = message.indexOf('parm1=');[/color]
That should be enough to tell you to start thinking at what you do. why are you ignoring the compiler warnings?
But as it's just a warning so it lets you compile et run the code. indeed you get
8
3
=
and then it all starts to makes sense
with the single quote you get the position 8 because the compiler (as evidence with the last print which shows '=') has transformed your illegal string 'parm1=' into just '=' --> so you are asking what is the index of the character '=' into "/y?parm1=98" which is indeed at index 8
if you use the correct notation, then you get 3 - because indeed 3 is the index in "/y?parm1=98" where "parm1=" starts.
don't try to mess around with adding or subtracting stuff to fix your programing mistakes or claim "after research there appears to be a bug in the String function"... that's total nonsense.
As often said - the bug had its hands on the keyboard and was staring at the screen...
(gentle joke)
Just program the right way.
if you want to look for "parm1=" and then point at the starting position of "98" then the right way to do this to meet the general case is
String message = "/y?parm1=98";
const char param1_string[] = "parm1=";
void setup() {
Serial.begin(115200);
int position = message.indexOf(param1_string) + strlen(param1_string);
Serial.print("Number will start at position "); Serial.println(position);
Serial.print("Number is "); Serial.println(message.c_str() + position); // message.c_str() = pointer to the array that contains a null-terminated sequence of characters
}
void loop() {}
and obviously if you could drop the String class altogether that would be even better for your memory footprint: the code above uses 3506 bytes and 262 of RAM.
this does the same thing:
char message[] = "/y?parm1=98";
const char param1_string[] = "parm1=";
void setup() {
Serial.begin(115200);
int position = strstr(message, param1_string)-message+strlen(param1_string);
Serial.print("Number will start at position "); Serial.println(position);
Serial.print("Number is "); Serial.println(message + position);
}
void loop() {}
and uses 2150 bytes of flash (you save 1356 bytes) and 246 bytes of RAM. (as tested on a MEGA)
of course in real life you would just do:
char message[] = "/y?parm1=98";
const char param1_string[] = "parm1=";
void setup() {
Serial.begin(115200);
int value = atoi(strstr(message, param1_string)+strlen(param1_string));
Serial.print("Number is "); Serial.println(value);
}
void loop() {}
and have the value of your param1 in an integer.
Makes sense ?