source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/08.Strings/StringComparisonOperators/StringComparisonOperators.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 3.2 KB
Line 
1/*
2  Comparing Strings
3
4 Examples of how to compare strings using the comparison operators
5
6 created 27 July 2010
7 modified 2 Apr 2012
8 by Tom Igoe
9
10 http://www.arduino.cc/en/Tutorial/StringComparisonOperators
11
12 This example code is in the public domain.
13 */
14
15String stringOne, stringTwo;
16
17void 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
33void 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
101  // comes first in 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}
Note: See TracBrowser for help on using the repository browser.