mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
Added compareTo() to comparison operator example
This commit is contained in:
@ -52,7 +52,7 @@ void loop() {
|
|||||||
Serial.println(stringOne + " >= " + stringTwo);
|
Serial.println(stringOne + " >= " + stringTwo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// comparison operators can be used to compare strings for alphabetic sorting too:
|
// comparison operators can be used to compare strings for alphabetic sorting too:
|
||||||
stringOne = String("Brown");
|
stringOne = String("Brown");
|
||||||
if (stringOne < "Charles") {
|
if (stringOne < "Charles") {
|
||||||
Serial.println(stringOne + " < Charles");
|
Serial.println(stringOne + " < Charles");
|
||||||
@ -71,8 +71,38 @@ void loop() {
|
|||||||
Serial.println(stringOne + " >= Brow");
|
Serial.println(stringOne + " >= Brow");
|
||||||
}
|
}
|
||||||
|
|
||||||
// do nothing while true:
|
// the compareTo() operator also allows you to compare strings
|
||||||
while(true);
|
// 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(0);
|
||||||
|
stringTwo += analogRead(5);
|
||||||
|
|
||||||
|
if (stringOne.compareTo(stringTwo) < 0 ) {
|
||||||
|
Serial.println(stringOne + " comes after " + stringTwo);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println(stringOne + " comes before " + stringTwo);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -80,3 +110,7 @@ void loop() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user