String.compareTo()

Description

Compare deux objets String, testant si l'un vient avant ou après l'autre, ou si ils sont égaux. Les chaînes de caractères sont comparées caractère par caractère, utilisant les valeurs ASCII des caractères. Ceci signifie par exemple, que 'a' vient avant 'b' mais après 'A'. Les nombres viennent avant les lettres.

Syntaxe

string.compareTo(string2)

Paramètres

  • string: une variable de type String()
  • string2: une autre variable de type String()

Valeurs Renvoyées

  • Un nombre négatif : si string vient avant string2
  • 0 : si string et string2 sont égaux
  • Un nombre positif : si string vient après string2

Exemple

String stringOne, stringTwo; void setup() { Serial.begin(9600); stringOne = String("this"); stringTwo = String("that"); Serial.println("\n\nComparing Strings:"); } 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 == 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 + " < Charles"); } if (stringOne > "Adams") { Serial.println(stringOne + " > Adams"); } if (stringOne <= "Browne") { Serial.println(stringOne + " <= Browne"); } 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); } } }
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.