Check if two strings are equal or not in Arduino

In order to check if two strings are equal, the .equals() function can be used. This returns a Boolean. True if both the strings are equal, else false. An example code is given below −


void setup() {
   // put your setup code here, to run once:
   String s1 = "Hello";
   String s2 = "Hello";
   String s3 = "World";
      Serial.print("s1 equals s2");
      Serial.print("s1 equals s3");
      Serial.print("s2 equals s3");
void loop() {
   // put your main code here, to run repeatedly:

As you would have guessed, only "s1 equals s2" gets printed on the Serial Monitor.


Updated on: 24-Mar-2021


Kickstart Your Career

Get certified by completing the course

Get Started