Pages: [1]   Go Down
Author Topic: endsWith Not working for me.  (Read 810 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8066
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (sensorReading.endsWith(0)) {

Perhaps you meant:
Code:
if (sensorReading.endsWith("0")) {

or maybe
Code:
if (sensorReading.endsWith('0')) {
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

UK
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8066
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

UK
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

%
Logged

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

UK
Offline Offline
Newbie
*
Karma: 1
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10426
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: