endsWith Not working for me.

I'm playing with the example sketchs using an Uno board and 1.01 and was looking at endsWith to read a string.
But the example doesn't work for me.
startsWith seems OK though.

Can anyone else confirm?
I get the following results regardless of whether the sensor ends with 0.

HTTP/1.1 200 OK
Server's using http version 1.1
Got an OK from the server
sensor = 10. This reading is not divisible by ten

String startsWith() and endsWith():
HTTP/1.1 200 OK
Server's using http version 1.1
Got an OK from the server
sensor = 28. This reading is not divisible by ten

String startsWith() and endsWith():
HTTP/1.1 200 OK
Server's using http version 1.1
Got an OK from the server
sensor = 4. This reading is not divisible by ten

String startsWith() and endsWith():
HTTP/1.1 200 OK
Server's using http version 1.1
Got an OK from the server
sensor = 0. This reading is not divisible by ten

The actual code from the demo.

Can anyone else confirm if I'm going mad.

/*
String startWith() and endsWith()

Examples of how to use startsWith() and endsWith() in a String

created 27 July 2010
by Tom Igoe

This example code is in the public domain.
*/

void setup() {
Serial.begin(9600);
Serial.println("\n\nString startsWith() and endsWith():");

}

void loop() {
// startsWith() checks to see if a String starts with a particular substring:
String stringOne = "HTTP/1.1 200 OK";
Serial.println(stringOne);
if (stringOne.startsWith("HTTP/1.1")) {
Serial.println("Server's using http version 1.1");
}

// you can also look for startsWith() at an offset position in the string:
stringOne = "HTTP/1.1 200 OK";
if (stringOne.startsWith("200 OK", 9)) {
Serial.println("Got an OK from the server");
}

// endsWith() checks to see if a String ends with a particular character:
String sensorReading = "sensor = ";
sensorReading += analogRead(0);
Serial.print (sensorReading);
if (sensorReading.endsWith(0)) {
Serial.println(". This reading is divisible by ten");
}
else {
Serial.println(". This reading is not divisible by ten");

}

// do nothing while true:
while(true);
}

 if (sensorReading.endsWith(0)) {

Perhaps you meant:

 if (sensorReading.endsWith("0")) {

or maybe

 if (sensorReading.endsWith('0')) {

Is it just me, but this seems over complicated, when a simple % operator would do the job?

John

Thanks for the quick reply.
This is the example sketch that came with the Arduino not code I have written

"0" doesn't compile
StringStartsWithEndsWith.cpp: In function 'void loop()':
StringStartsWithEndsWith:33: error: invalid conversion from 'const char*' to 'uint8_t'
StringStartsWithEndsWith:33: error: initializing argument 1 of 'int analogRead(uint8_t)'

and '0' doesn't work.
It is possible you could try it and see if the function is actually working.

123tcpip:
"0" doesn't compile
StringStartsWithEndsWith.cpp: In function 'void loop()':
StringStartsWithEndsWith:33: error: invalid conversion from 'const char*' to 'uint8_t'
StringStartsWithEndsWith:33: error: initializing argument 1 of 'int analogRead(uint8_t)'

Compiled and work fine for me in this code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  String sensorReading = "sensor = ";
  sensorReading += analogRead(A0);
  Serial.print (sensorReading);
  if (sensorReading.endsWith("0")) {
    Serial.println(". This reading is divisible by ten"); 
  } 
  else {
    Serial.println(". This reading is not divisible by ten"); 
  }
  delay(250);
}

As I expected sensorReading.endsWith(0) did not work.

The argument to endsWith() is a "const String reference". I think it tries to take the argument, in this case a constant 0, and construct a String out of it. In theory the constant should be treated as an 'int' and should thus be converted to a decimal String "0". I think that the constant 0 may be taken as a 'char'. In that case the a two-byte buffer would be constructed with the character (null) and a null terminator. The resulting string would be empty. I suspect that is what is happening and why the "endsWith(0)" always return false.

Perhaps (int)0 would work.

I still think "% 10" is a much simpler solution.

John

Sorry I changed the wrong 0 in my rush to test and get out of the house last night and as you say it compiles and work fine if I use "0"
As this is the distributed example is it worth letting Arduino know?

AWOL

Could you explain %10 as I was unable to find any reference to it.
I assume it's means if it's divisible by 10 but I can't work out the syntax

Rob

%

AWOL

Thanks for the pointer I couldn't get a result searching for % on the web or board.
I will go and have a play.

Rob

johnwasser:
The argument to endsWith() is a "const String reference". I think it tries to take the argument, in this case a constant 0, and construct a String out of it. In theory the constant should be treated as an 'int' and should thus be converted to a decimal String "0". I think that the constant 0 may be taken as a 'char'. In that case the a two-byte buffer would be constructed with the character (null) and a null terminator. The resulting string would be empty. I suspect that is what is happening and why the "endsWith(0)" always return false.

Perhaps (int)0 would work.

In C and C++ anywhere a pointer or reference is accepted the null pointer is also accepted. The null pointer is notated as 0.

In C and C++ anywhere a pointer or reference is accepted the null pointer is also accepted. The null pointer is notated as 0.

Pointers can be NULL (or 0) but references can not.

In this case I believe the compiler decided it could take your 0 and turn it into a string. However, looking at the reference for String, this should work.