- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Strings in Arduino
Arduino has an inbuilt compareTo() function that helps compare which string comes before another. Very crudely, you can think of it as this: if you are given two strings, which one will come first in a dictionary.
Syntax
String1.compareTo(String2)
Where String1 and String2 are the two strings to be compared. This function returns an integer. Here’s the interpretation of the value of the integer −
Negative − String1 comes before String2
0 − String1 and String2 are equal
Positive − String2 comes before String1
Please note that this function is case sensitive. So ‘A’ comes before ‘a’ and ‘B’ comes before ‘a’. But ‘a’ comes before ‘b’. Also, numbers come before letters. Basically, if a character’s ASCII value is higher than another, then the higher character comes later in the dictionary. And compareTo() function compares the strings character by character.
Example
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); String s1 = "Book"; String s2 = "books"; String s3 = "library"; if(s1.compareTo(s2) < 0){ Serial.println("s1 before s2"); } if(s2.compareTo(s3) < 0){ Serial.println("s2 before s3"); } if(s3.compareTo(s1) < 0){ Serial.println("s3 before s1"); } } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is shown below −
As you can see, the function works exactly as described.
- Related Articles
- Concatenate strings in Arduino
- Compare two Strings in Java
- Compare date strings in MySQL
- Compare two strings lexicographically in Java.
- How to compare strings in Java?
- Compare two strings lexicographically in C#
- Java Program to Compare Strings
- Compare two Strings lexicographically in Java programming
- How to compare date strings in Python?
- How do I compare strings in Java?
- Compare the two Strings lexicographically in Java
- Program to Compare two strings in Java
- How to compare two strings in Golang?
- Java Program to Compare Two Strings
- How to initialize and compare strings?
