How to Compare Strings in Arduino
-
Compare Strings Using the
compareTo()
Function in Arduino -
Compare Strings Using the
equals()
Function in Arduino - Compare Strings Using Comparison Operators in Arduino
This tutorial will discuss comparing two strings using the compareTo()
function in Arduino.
Compare Strings Using the compareTo()
Function in Arduino
To compare two strings in Arduino, we can use the string object’s compareTo()
function. The compareTo()
function compares the characters present in the two strings one after the other to identify if the two strings are the same or not.
The compareTo()
function starts from the first character of both the strings and compares them using their ASCII values, and if the strings match, it will move to the next character. Each keyboard character has a unique ASCII value.
The ASCII values of upper and lower case letters are different; the ASCII value of a
is 97, and A
is 65. If we want to compare the two strings ignoring the upper and lowercase, we must make sure both the strings are in the upper or lower case.
To convert a string to uppercase, we can use the toUpperCase()
function of the string object in Arduino. To convert a string to lower case, we can use the toLowerCase()
function of the string object in Arduino.
The compareTo()
function returns 0 if the two strings are equal. The compareTo()
function returns a negative number if the ASCII value of the first string is less than the ASCII value of the second string.
The compareTo()
function returns a positive number if the ASCII value of the first string is greater than the ASCII value of the second string. For example, let’s define two string variables and compare them using the compareTo()
function in Arduino.
String string_1 = "Hello";
String string_2 = "World";
int result = 0;
void setup() { result = string_1.compareTo(string_2); }
void loop() {}
The comparison result will be stored inside the result
variable. We can also use the equals()
function instead of the compareTo()
function.
Compare Strings Using the equals()
Function in Arduino
The equals()
function returns a boolean, which will be true if the two strings are equal and false if they are not. The equals()
function is also case sensitive, which means abc
will not be equal to ABC
.
Check this link for more details about the equals()
function. If we want to compare two strings neglecting the upper or lower case sensitivity, we can use the string object’s equalsIgnoreCase()
function.
The equalsIgnoreCase()
also returns a boolean and its not case sensitive which mean abc
and ABC
will be equal and the function will return true. Check this link for more details about the equalsIgnoreCase()
function.
Compare Strings Using Comparison Operators in Arduino
We can also use the comparison operators like less than or equal to <=
, greater than or equal to >=
, equal to ==
, and other operators to compare strings.
In this case, the strings will be compared according to the ASCII values of the characters present in them, which means this comparison will also be case-sensitive.
For example, the below statement will return false.
"hello" == "Hello"