Go Down

Topic: String library bug? startsWith always returns true (Read 1 time) previous topic - next topic



apparently I've run into a bug in regards to the String library.
The odd part is, I've used the startsWith method before, and it worked a treat.
But whenever I try to use it now, it always returns true.

I tried it with the old v0.4 library, the v0.8 library (both the one I had, and a fresh download), but it keeps returning true.
Won't include the library in this post, as I've been using an unmodified version of the one found on this site.

Sketch used for testing purposes:
Code: [Select]
#include <WString.h>

void setup()
 String test = "foo";
 if(test.startsWith("f")) { Serial.println("starts with f"); }
 if(test.startsWith("bar")) { Serial.println("starts with bar"); }
 if(test.startsWith("far too large a string")) { Serial.println("I'm stumped"); }

void loop()

I use ubuntu 9.10, and I have a feeling it has something to do with an update or so (the usual culprit when something stops working for no apparent reason), but it remains quite annoying.
The arduino I'm using is a DFRduino duemilanove with Atmega328 chip.

Anybody have an idea for a work-around, fix or cause to this bug?


Apr 13, 2010, 08:04 am Last Edit: Apr 13, 2010, 08:30 am by TBAr Reason: 1
I ran your example on Ubuntu 9.10 (with Arduino IDE version 18, String library version 0.8 just downloaded) and I got the same results you did.

I then ran it under Windows/XP (with Arduino IDE version 17, also String library version 0.8) and got the same results there.

I deleted all the *string*.o files I could find, re-compiled and re-uploaded.

I ran both tests with a stock Arduino Duemilanove w/Atmega328, genuine "Made in Italy".

I tried some of the examples that are distributed with the library, and none of the methods seem to work under either environment.

The setup() code from the StringEndsWith.pde example (modified to highlight the version):
Code: [Select]
#include <WString.h>                // include the String library

#define maxLength 30

String inString = String(maxLength);       // allocate a new String

void setup() {
 // open the serial port:
 // Say hello:
 Serial.print("String Library version: ->");

void loop() {


Code: [Select]
String Library version: -><-

Can anyone else run these simple examples, whatever your environment is?


And just as suddenly as this bug appeared, it vanishes?
I copy-pasted the old v0.4 library back in my 018 folder, and suddenly it works again (was going to give it another round of debugging).

This makes me wonder, this would imply that the error isn't in the code..
Could this bug be related to the IDE? like the 'my program disappeared!' bug mentioned here?


May 18, 2010, 02:16 pm Last Edit: May 18, 2010, 02:17 pm by abstractor Reason: 1
This is becouse the string library contains a bug in the constructor. For example:

Code: [Select]
String test = "Foo";
Serial.println(test.length); // Prints 0
Serial.println(test.cstr()); // Prints foo

It can be fixed by changing String.cpp as follows:

Code: [Select]
String::String(const char* bytes)
 if(bytes == NULL)
   bytes= "";
 _length = strlen(bytes);
 _capacity = _length+1;
 _array = (char*)malloc(_capacity);

the call to clear sets the length of the string to 0.


Code: [Select]
String::String(const char* bytes)
 if(bytes == NULL)
   bytes= "";
 _length = strlen(bytes);
 //if (_capacity < _length) {
 _capacity = _length;
 //  free(_array);
 _array = (char*)malloc(_length+1);

The constructor in my version appears to be fine.
The clear() method sets the entire array to null characters, it doesn't reset the length.
So it resets the array, then writes the argument to the array.
Appears to be solid code  :-/
Still no idea what caused this bug, it disappeared as randomly as it appeared.


Thanks for the discoveries, and sorry for my delay in replying to this.  We've never included String in the core libraries partially because it's always been a little buggy and not ready for prime time.  One of my colleagues and I are doing some work to improve that in the coming weeks, though, and I hope we'll have a new version up soon.


...and the length bugfixes are already in 0.9, posted now.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131