Go Down

Topic: endsWith Not working for me. (Read 1 time) previous topic - next topic

123tcpip

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);
}

johnwasser

Code: [Select]

if (sensorReading.endsWith(0)) {


Perhaps you meant:
Code: [Select]

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


or maybe
Code: [Select]

if (sensorReading.endsWith('0')) {
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AWOL

Is it just me, but this seems over complicated, when a simple % operator would do the job?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

123tcpip

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.


James C4S


"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:

Code: [Select]

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

AWOL

I still think "% 10" is a much simpler solution.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

123tcpip

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

123tcpip

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

MarkT


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.
[ I won't respond to messages, use the forum please ]

Quote
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.

Go Up