4
Examples of how to compare strings using the comparison operators
10
http://arduino.cc/en/Tutorial/StringComparisonOperators
12
This example code is in the public domain.
15
String stringOne, stringTwo;
19
stringOne = String("this");
20
stringTwo = String("that");
21
Serial.println("\n\nComparing Strings:");
27
if (stringOne == "this") {
28
Serial.println("StringOne == \"this\"");
30
// two strings not equal:
31
if (stringOne != stringTwo) {
32
Serial.println(stringOne + " =! " + stringTwo);
35
// two strings not equal (case sensitivity matters):
38
if (stringOne != stringTwo) {
39
Serial.println(stringOne + " =! " + stringTwo);
41
// you can also use equals() to see if two strings are the same:
42
if (stringOne.equals(stringTwo)) {
43
Serial.println(stringOne + " equals " + stringTwo);
46
Serial.println(stringOne + " does not equal " + stringTwo);
49
// or perhaps you want to ignore case:
50
if (stringOne.equalsIgnoreCase(stringTwo)) {
51
Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
54
Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
57
// a numeric string compared to the number it represents:
60
if (stringOne == numberOne) {
61
Serial.println(stringOne + " = " + numberOne);
66
// two numeric strings compared:
69
if (stringOne >= stringTwo) {
70
Serial.println(stringOne + " >= " + stringTwo);
73
// comparison operators can be used to compare strings for alphabetic sorting too:
74
stringOne = String("Brown");
75
if (stringOne < "Charles") {
76
Serial.println(stringOne + " < Charles");
79
if (stringOne > "Adams") {
80
Serial.println(stringOne + " > Adams");
83
if (stringOne <= "Browne") {
84
Serial.println(stringOne + " <= Browne");
88
if (stringOne >= "Brow") {
89
Serial.println(stringOne + " >= Brow");
92
// the compareTo() operator also allows you to compare strings
93
// it evaluates on the first character that's different.
94
// if the first character of the string you're comparing to
95
// comes first in alphanumeric order, then compareTo() is greater than 0:
96
stringOne = "Cucumber";
97
stringTwo = "Cucuracha";
98
if (stringOne.compareTo(stringTwo) < 0 ) {
99
Serial.println(stringOne + " comes before " + stringTwo);
102
Serial.println(stringOne + " comes after " + stringTwo);
105
delay(10000); // because the next part is a loop:
107
// compareTo() is handy when you've got strings with numbers in them too:
110
stringOne = "Sensor: ";
111
stringTwo= "Sensor: ";
113
stringOne += analogRead(A0);
114
stringTwo += analogRead(A5);
116
if (stringOne.compareTo(stringTwo) < 0 ) {
117
Serial.println(stringOne + " comes before " + stringTwo);
120
Serial.println(stringOne + " comes after " + stringTwo);
b'\\ No newline at end of file'