~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to build/shared/examples/8.Strings/StringComparisonOperators/StringComparisonOperators.ino

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Comparing Strings 
 
3
 
 
4
 Examples of how to compare strings using the comparison operators
 
5
 
 
6
 created 27 July 2010
 
7
 modified 30 Aug 2011
 
8
 by Tom Igoe
 
9
 
 
10
 http://arduino.cc/en/Tutorial/StringComparisonOperators
 
11
 
 
12
 This example code is in the public domain.
 
13
 */
 
14
 
 
15
String stringOne, stringTwo;
 
16
 
 
17
void setup() {
 
18
  Serial.begin(9600);
 
19
  stringOne = String("this");
 
20
  stringTwo = String("that");
 
21
  Serial.println("\n\nComparing Strings:");
 
22
 
 
23
}
 
24
 
 
25
void loop() {
 
26
  // two strings equal:
 
27
  if (stringOne == "this") {
 
28
    Serial.println("StringOne == \"this\""); 
 
29
  }
 
30
  // two strings not equal:
 
31
  if (stringOne != stringTwo) {
 
32
    Serial.println(stringOne + " =! " + stringTwo);
 
33
  }
 
34
 
 
35
  // two strings not equal (case sensitivity matters):
 
36
  stringOne = "This";
 
37
  stringTwo = "this";
 
38
  if (stringOne != stringTwo) {
 
39
    Serial.println(stringOne + " =! " + stringTwo);
 
40
  }
 
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);
 
44
  } 
 
45
  else {
 
46
    Serial.println(stringOne + " does not equal " + stringTwo);
 
47
  }
 
48
 
 
49
  // or perhaps you want to ignore case:
 
50
  if (stringOne.equalsIgnoreCase(stringTwo)) {
 
51
    Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
 
52
  } 
 
53
  else {
 
54
    Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
 
55
  }
 
56
 
 
57
  // a numeric string compared to the number it represents:
 
58
  stringOne = "1";
 
59
  int numberOne = 1;
 
60
  if (stringOne == numberOne) {
 
61
    Serial.println(stringOne + " = " + numberOne);
 
62
  }
 
63
 
 
64
 
 
65
 
 
66
  // two numeric strings compared:
 
67
  stringOne = "2";
 
68
  stringTwo = "1";
 
69
  if (stringOne >= stringTwo) {
 
70
    Serial.println(stringOne + " >= " + stringTwo);
 
71
  }
 
72
 
 
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"); 
 
77
  }
 
78
 
 
79
  if (stringOne > "Adams") {
 
80
    Serial.println(stringOne + " > Adams"); 
 
81
  }
 
82
 
 
83
  if (stringOne <= "Browne") {
 
84
    Serial.println(stringOne + " <= Browne"); 
 
85
  }
 
86
 
 
87
 
 
88
  if (stringOne >= "Brow") {
 
89
    Serial.println(stringOne + " >= Brow"); 
 
90
  }
 
91
 
 
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); 
 
100
  } 
 
101
  else {
 
102
    Serial.println(stringOne + " comes after " + stringTwo);    
 
103
  }
 
104
 
 
105
  delay(10000);  // because the next part is a loop:
 
106
 
 
107
  // compareTo() is handy when you've got strings with numbers in them too:
 
108
 
 
109
  while (true) {
 
110
    stringOne = "Sensor: ";
 
111
    stringTwo= "Sensor: ";
 
112
 
 
113
    stringOne += analogRead(A0); 
 
114
    stringTwo += analogRead(A5);
 
115
 
 
116
    if (stringOne.compareTo(stringTwo) < 0 ) {
 
117
      Serial.println(stringOne + " comes before " + stringTwo); 
 
118
    } 
 
119
    else {
 
120
      Serial.println(stringOne + " comes after " + stringTwo); 
 
121
 
 
122
    }
 
123
  }
 
124
}
 
 
b'\\ No newline at end of file'