Wstring question

Hello,

I use this library: String() - Arduino Reference

It is made using the wiring string library: http://wiring.org.co/learning/reference/String_equals_.html

When I try this code, nothing happens (the message "Equal" should constantly be printed. There seems to occur an error. The stange thing is that this example is copied from the wiring documentation (see link)

#include <WString.h>                // include the String library

String str1 = "CCCP"; 
String str2 = "CCCP"; 
// Tests to see if str1 is equal to str2 

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

} 

void loop() { 

  if(str1.equals(str2) == true) { 
    Serial.println("Equal");  // They are equal so this line will print 
  } 
  else { 
    Serial.println("Not equal"); 
  } 

}

When I try this code, everything works fine:

String str1 = "CCCP";   
char str3[] = "CCCP"; 
// Tests to see if str1 is equal to str3   
 
void setup() { 
  Serial.begin(9600);  
} 
 
void loop() { 
  if(str1.equals(str3) == true) {   
    Serial.println("Equal");  // They are equal so this line will print   
  }  
  else {   
    Serial.println("Not equal");   
  }  
}

Is is not possible to declare the string that is compared as string, but only as char in Arduino?

Thanks in advance.

When I combine the code:

#include <WString.h>

String str1 = "CCCP";   
char str3[] = "CCCP"; 

String Test = "CCCP";  

// Tests to see if str1 is equal to str3   

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

} 

void loop() { 

  if(str1.equals(str3) == true) {   
    Serial.println("Equalstr3");  // They are equal so this line will print   
  }  
  else {   
    Serial.println("Not equal");   
  }  

  delay(1000);

  if(str1.equals(Test) == true) {  // Testing directly 
    Serial.println("EqualTest");  // They are equal so this line will print   
  }  
  else {   
    Serial.println("Not equal");   
  }  

  delay(2000);

}

The code uploads without any problems. When I open the serial monitor I get the message for the first If....Then ( prints "Equalstr3"), but for the next If....Then nothing happens.

No messages are printed anymore, so the program hangs at the second If....Then.

Maybe I have to contact the person who wrote this library. Maybe this is not possible.

I really liked the idea of comparing two stings because I want to compare the content of several strings.

Any ideas?

learn to use the avr-libc's string.h and stdlib.h
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html

from arduino, you need to include this using

extern "C" {
#include <stdlib.h>
#include <string.h>
}

you need stdlib.h so you can allocate memory for your strings, and string.h to use the string compare function

doing this yourself without that library is much more efficient, just remember that every malloc() or calloc() should have a matching free()

char str1[] = "test";
const char * str2 = "test";

if (strcmp(str1, str2) == 0)
    Serial.println("Equal");

char * str3 = calloc(4 + 1, sizeof(char));
str3 = strcpy(str3, str2);

if (strcmp(str3, str2) == 0)
    Serial.println("Equal");

free(str3);

Thanks for you suggestion :slight_smile:

As far as I know the WString library offers more advanced functions (that I would like to use) like:

void setCharAt(int positon, char thisChar) - sets the character specified at position to thisChar

Why is this library (string.h) more efficient?

Thanks in advance.

setcharat is advanced?

const char * str = "abczefg";
str[3] = 'd';

that's not even a function, that's just how C works, a string is absolutely NO DIFFERENT from an array, except that it is null terminated to indicate where it ends.

all WString.h does is be a wrapper for string.h and stdlib.h

void String::setCharAt(int charNum, char thisChar)
{
  if (_length > charNum) {      
    _array[charNum] = thisChar;
  }
}

all this adds is a check so you don't write outside your array, you can easily retrieve the length yourself by using strlen() from string.h

Ok makes sense :), you've almost convinced me!

Why did you use : const char * str = "abczefg"; in you code?

I recently started with the C language (because of arduino), but why did you used a pointer in this example (it is a pointer, isn't it?)?

I still don't get Pointers (I am used to VB .NET).

Thanks for the help.

Cheers

It's a great library and makes string manipulation much easier, but there is a bug in the posted library. Please see this thread for the patch:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241618944

I have contacted the library author and hoping he patches it soon.

Frank is correct that there is no magic happening, but for me the purpose is to make things easier and cleaner. I think it achieves that.

const char * is how you declare a string, char * is the pointer , and the const tells the compiler not to expect this pointer to move, by knowing that the pointer won't move around, the compiler can allocate the memory during compile time (as opposed to using malloc() and calloc() which has to place the pointer to a location with enough free space during run time).

const char * str = "abc";

is identical to

char str[4] = {'a', 'b', 'c', 0};

and

volatile int length = 4; // volatile to avoid optimization
char str[length];

is the same as

volatile int length = 4;
char * str = malloc(sizeof(char) * length);

just so you can relate pointers to arrays, which you are probably more familiar with

I'm just a guy who'd sacrifice convenience for code size. Also I would hate to run into heap-stack crashes because of somebody else's mistake, with a library like WString, you'd have a tendency to assume that it's reliable