JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Comparing Strings in Arduino

Author:JIYIK Last Updated:2025/04/16 Views:

compareTo()This tutorial will discuss comparing two strings using the function in Arduino .


compareTo()Comparing strings using the Arduino function

To compare two strings in Arduino, we can use compareTo()the function of the string object. compareTo()The function compares the characters present in both the strings one by one to find out whether the two strings are the same.

compareTo()The function starts at the first character of both strings and compares them using their ASCII values, if the strings match it moves on to the next character. Each keyboard character has a unique ASCII value.

The ASCII values ​​of uppercase and lowercase letters are different; athe ASCII value of is 97 and Athe value of is 65. If we want to compare two strings ignoring case, we must ensure that both strings are either uppercase or lowercase.

To convert a string to uppercase, we can use the function of the string object in Arduino toUpperCase(). To convert a string to lowercase, we can use toLowerCase()the function of the string object in Arduino.

If the two strings are equal, compareTo()the function returns 0. If the ASCII value of the first string is less than the ASCII value of the second string, compareTo()the function returns a negative number.

If the ASCII value of the first string is greater than the ASCII value of the second string, the function returns a positive number. For example, let's define two string variables and compare them compareTo()using the function in Arduino .compareTo()

String string_1 = "Hello";
String string_2 = "World";
int result = 0;

void setup() { result = string_1.compareTo(string_2); }
void loop() {}

The result of the comparison will be stored resultin the variable. We can also use equals()the function instead of compareTo()the function.


equals()Comparing strings using the Arduino function

equals()The function returns a boolean value, true if the two strings are equal, false otherwise. equals()The function is also case sensitive, which means that abcwill not be equal ABC.

equals()Check out this link for more details about the compare function. If we want to compare two strings ignoring case sensitivity, we can use the compare equalsIgnoreCase()function of the string object.

equalsIgnoreCase()also returns a boolean value which is case insensitive, meaning abcand ABCwill be equal and the function will return true. Check this link for equalsIgnoreCase()more details on the function.


Comparing Strings Using Comparison Operators in Arduino

We can also use comparison operators such as less than or equal to <=, greater than or >=equal to, equal ==to to compare strings.

In this case, the strings are compared based on the ASCII values ​​of the characters present in them, which means that the comparison will also be case-sensitive.

For example, the following statement will return false.

"hello" == "Hello"

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino Square Wave Generator

Publish Date:2025/04/16 Views:181 Category:C++

digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values ​​are instan

Splitting a string in Arduino

Publish Date:2025/04/16 Views:188 Category:C++

substring() This tutorial will discuss splitting a string using the function in Arduino . substring() Splitting a string in Arduino using the function Arduino provides an inbuilt function substring() to split a given string. We can split th

Arduino mills() function

Publish Date:2025/04/16 Views:156 Category:C++

This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial