From 8a21cd9e9e11a49bd4031d6378d42c81d1f13a2b Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 27 Jul 2010 20:02:29 +0000 Subject: [PATCH] Added compareTo() to comparison operator example --- .../StringComparisonOperators.pde | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde b/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde index 5cb891250..b4e5c2df2 100644 --- a/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde +++ b/build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.pde @@ -52,7 +52,7 @@ void loop() { 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"); if (stringOne < "Charles") { Serial.println(stringOne + " < Charles"); @@ -71,8 +71,38 @@ void loop() { Serial.println(stringOne + " >= Brow"); } - // do nothing while true: - while(true); + // 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(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() { + + + +