compare serial input with string

How to compare a string coming from serial monitor with some predefined text stored as a local variable? if i say:

int led = 2;
String a = " abcds";
void setup(){

void loop(){
String b =;

if(b != a){

just as an example, this code will not compile because on the serial i receive bytes and i want to compare with a string. So my question is... how should be done?

Thanks in advance !

Serial.fead() reads 1 byte, not the whole String. You could use Serial.readString(). Better is to not use Strings at all, but read into a null terminated character array (string, note small s). The serial input basics tutorial shows how.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with ‘\0’ (NULL).

When using Cstrings you must use strcmp() to compare values rather than ==

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.


Serial.fead() reads 1 byte,

Are you sure about that?