ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## String Comparison Operators The [String](https://www.arduino.cc/en/Reference/StringObject) comparison operators `==`, `!=`,`>`, `<` ,`>=`, `<=` , and the `equals()` and `equalsIgnoreCase()`methods allow you to make alphabetic comparisons between Strings. They're useful for sorting and alphabetizing, among other things. The operator `==` and the method `equals()` perform identically. In other words, if (stringOne.equals(stringTwo)) { is identical to if (stringOne ==stringTwo) { The ">" (greater than) and "<" (less than) operators evaluate strings in alphabetical order, on the first character where the two differ. So, for example `"a" < "b"` and `"1" < "2"`, but `"999" > "1000"` because 9 comes after 1. Caution: String comparison operators can be confusing when you're comparing numeric strings, because the numbers are treated as strings and not as numbers. If you need to compare numbers, compare them as ints, floats, or longs, and not as Strings. ### Hardware Required * Arduino or Genuino Board ### Circuit There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open. [![](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png)](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png) image developed using [Fritzing](http://www.fritzing.org/). For more circuit examples, see the [Fritzing project page](http://fritzing.org/projects/) ### Code /*   Comparing Strings   Examples of how to compare Strings using the comparison operators   created 27 Jul 2010   modified 2 Apr 2012   by Tom Igoe   This example code is in the public domain.   http://www.arduino.cc/en/Tutorial/StringComparisonOperators */ String stringOne, stringTwo; void setup() {   // Open serial communications and wait for port to open:   Serial.begin(9600);   while (!Serial) {     ; // wait for serial port to connect. Needed for native USB port only   }   stringOne = String("this");   stringTwo = String("that");   // send an intro:   Serial.println("\n\nComparing Strings:");   Serial.println(); } void loop() {   // two Strings equal:   if (stringOne == "this") {     Serial.println("StringOne == \"this\"");   }   // two Strings not equal:   if (stringOne != stringTwo) {     Serial.println(stringOne + " =! " + stringTwo);   }   // two Strings not equal (case sensitivity matters):   stringOne = "This";   stringTwo = "this";   if (stringOne != stringTwo) {     Serial.println(stringOne + " =! " + stringTwo);   }   // you can also use equals() to see if two Strings are the same:   if (stringOne.equals(stringTwo)) {     Serial.println(stringOne + " equals " + stringTwo);   } else {     Serial.println(stringOne + " does not equal " + stringTwo);   }   // or perhaps you want to ignore case:   if (stringOne.equalsIgnoreCase(stringTwo)) {     Serial.println(stringOne + " equals (ignoring case) " + stringTwo);   } else {     Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);   }   // a numeric String compared to the number it represents:   stringOne = "1";   int numberOne = 1;   if (stringOne.toInt() == numberOne) {     Serial.println(stringOne + " = " + numberOne);   }   // two numeric Strings compared:   stringOne = "2";   stringTwo = "1";   if (stringOne >= stringTwo) {     Serial.println(stringOne + " >= " + stringTwo);   }   // comparison operators can be used to compare Strings for alphabetic sorting too:   stringOne = String("Brown");   if (stringOne  "Charles") {     Serial.println(stringOne + " );   }   if (stringOne > "Adams") {     Serial.println(stringOne + " > Adams");   }   if (stringOne  "Browne") {     Serial.println(stringOne + " );   }   if (stringOne >= "Brow") {     Serial.println(stringOne + " >= Brow");   }   // the compareTo() operator also allows you to compare Strings   // it evaluates on the first character that's different.   // if the first character of the String you're comparing to comes first in   // alphanumeric order, then compareTo() is greater than 0:   stringOne = "Cucumber";   stringTwo = "Cucuracha";   if (stringOne.compareTo(stringTwo)  0) {     Serial.println(stringOne + " comes before " + stringTwo);   } else {     Serial.println(stringOne + " comes after " + stringTwo);   }   delay(10000);  // because the next part is a loop:   // compareTo() is handy when you've got Strings with numbers in them too:   while (true) {     stringOne = "Sensor: ";     stringTwo = "Sensor: ";     stringOne += analogRead(A0);     stringTwo += analogRead(A5);     if (stringOne.compareTo(stringTwo)  0) {       Serial.println(stringOne + " comes before " + stringTwo);     } else {       Serial.println(stringOne + " comes after " + stringTwo);     }   } } [[Get Code]](https://www.arduino.cc/en/Tutorial/StringComparisonOperators?action=sourceblock&num=1) ### See Also * [String object](https://www.arduino.cc/en/Reference/StringObject) – Your Reference for String objects * [CharacterAnalysis](https://www.arduino.cc/en/Tutorial/CharacterAnalysis) - We use the operators that allow us to recognise the type of character we are dealing with. * [StringAdditionOperator](https://www.arduino.cc/en/Tutorial/StringAdditionOperator) - Add strings together in a variety of ways. * [StringAppendOperator](https://www.arduino.cc/en/Tutorial/StringAppendOperator) - Use the += operator and the concat() method to append things to Strings * [StringCaseChanges](https://www.arduino.cc/en/Tutorial/StringCaseChanges) - Change the case of a string. * [StringCharacters](https://www.arduino.cc/en/Tutorial/StringCharacters) - Get/set the value of a specific character in a string. * [StringConstructors](https://www.arduino.cc/en/Tutorial/StringConstructors) - Initialize string objects. * [StringIndexOf](https://www.arduino.cc/en/Tutorial/StringIndexOf) - Look for the first/last instance of a character in a string. * [StringLength](https://www.arduino.cc/en/Tutorial/StringLength) - Get the length of a string. * [StringLengthTrim](https://www.arduino.cc/en/Tutorial/StringLengthTrim) - Get and trim the length of a string. * [StringReplace](https://www.arduino.cc/en/Tutorial/StringReplace) - Replace individual characters in a string. * [StringStartsWithEndsWith](https://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith) - Check which characters/substrings a given string starts or ends with. * [StringSubstring](https://www.arduino.cc/en/Tutorial/StringSubstring) - Look for "phrases" within a given string. * [StringToInt](https://www.arduino.cc/en/Tutorial/StringToInt) - Allows you to convert a String to an integer number.