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

void setup() { Serial.begin(9600); String myString = "Arduino"; String myString2 = "ArduinoGetStarted.com"; int result = myString.compareTo(myString2); Serial.println(result); } void loop() { }

The result on Serial Monitor:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
-71
Ln 11, Col 1
Arduino Uno on COM15
2

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES