Debian Codegraph @

examples/08.Strings/StringComparisonOperators/StringComparisonOperators.ino

raw
1 /*
2 Comparing Strings
3
4 Examples of how to compare Strings using the comparison operators
5
6 created 27 Jul 2010
7 modified 2 Apr 2012
8 by Tom Igoe
9
10 This example code is in the public domain.
11
12 https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringComparisonOperators
13 */
14
15 String stringOne, stringTwo;
16
17 void setup() {
18 // Open serial communications and wait for port to open:
19 Serial.begin(9600);
20 while (!Serial) {
21 ; // wait for serial port to connect. Needed for native USB port only
22 }
23
24
25 stringOne = String("this");
26 stringTwo = String("that");
27 // send an intro:
28 Serial.println("\n\nComparing Strings:");
29 Serial.println();
30
31 }
32
33 void loop() {
34 // two Strings equal:
35 if (stringOne == "this") {
36 Serial.println("StringOne == \"this\"");
37 }
38 // two Strings not equal:
39 if (stringOne != stringTwo) {
40 Serial.println(stringOne + " =! " + stringTwo);
41 }
42
43 // two Strings not equal (case sensitivity matters):
44 stringOne = "This";
45 stringTwo = "this";
46 if (stringOne != stringTwo) {
47 Serial.println(stringOne + " =! " + stringTwo);
48 }
49 // you can also use equals() to see if two Strings are the same:
50 if (stringOne.equals(stringTwo)) {
51 Serial.println(stringOne + " equals " + stringTwo);
52 } else {
53 Serial.println(stringOne + " does not equal " + stringTwo);
54 }
55
56 // or perhaps you want to ignore case:
57 if (stringOne.equalsIgnoreCase(stringTwo)) {
58 Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
59 } else {
60 Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
61 }
62
63 // a numeric String compared to the number it represents:
64 stringOne = "1";
65 int numberOne = 1;
66 if (stringOne.toInt() == numberOne) {
67 Serial.println(stringOne + " = " + numberOne);
68 }
69
70
71
72 // two numeric Strings compared:
73 stringOne = "2";
74 stringTwo = "1";
75 if (stringOne >= stringTwo) {
76 Serial.println(stringOne + " >= " + stringTwo);
77 }
78
79 // comparison operators can be used to compare Strings for alphabetic sorting too:
80 stringOne = String("Brown");
81 if (stringOne < "Charles") {
82 Serial.println(stringOne + " < Charles");
83 }
84
85 if (stringOne > "Adams") {
86 Serial.println(stringOne + " > Adams");
87 }
88
89 if (stringOne <= "Browne") {
90 Serial.println(stringOne + " <= Browne");
91 }
92
93
94 if (stringOne >= "Brow") {
95 Serial.println(stringOne + " >= Brow");
96 }
97
98 // the compareTo() operator also allows you to compare Strings
99 // it evaluates on the first character that's different.
100 // if the first character of the String you're comparing to comes first in
101 // alphanumeric order, then compareTo() is greater than 0:
102 stringOne = "Cucumber";
103 stringTwo = "Cucuracha";
104 if (stringOne.compareTo(stringTwo) < 0) {
105 Serial.println(stringOne + " comes before " + stringTwo);
106 } else {
107 Serial.println(stringOne + " comes after " + stringTwo);
108 }
109
110 delay(10000); // because the next part is a loop:
111
112 // compareTo() is handy when you've got Strings with numbers in them too:
113
114 while (true) {
115 stringOne = "Sensor: ";
116 stringTwo = "Sensor: ";
117
118 stringOne += analogRead(A0);
119 stringTwo += analogRead(A5);
120
121 if (stringOne.compareTo(stringTwo) < 0) {
122 Serial.println(stringOne + " comes before " + stringTwo);
123 } else {
124 Serial.println(stringOne + " comes after " + stringTwo);
125
126 }
127 }
128 }