-1

If there's one thing I never managed to do: it's comparing strings. So I have the incoming message saved here

String Message2 = IClient[i].readStringUntil('\n');

And every time I try to compare it with

strcmp(Message2.c_str(),"Red"); //Hopefully comparing the Message2 with "Red"

I never get 0 returned.

I know from printing the Message2 to Serial that Message2 contains the word "Red".

I've tried using different methods (string.equal(string2); etc.) but it never said that they are equal. What am I missing out on?

1
  • Might want to remove the extra characters ("..."). Or compare with "\"Red\"". – Mikael Patel Mar 20 '19 at 21:56
0

Found the answer!

After testing around with changing the terminator of the readStringUntil('\n') method it appears that there were leading and trailing white spaces, though not being displayed on the serial monitor they still had an impact on the strcmp(string1, string2) method.

I could solve this problem by "trimming" the received string before comparing it by using the String1.trim(); method.

In the end looking like:

String Message2 = IClient[i].readStringUntil('\n');

Message2.trim();

strcmp(Message2.c_str(),"Red"); 
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.