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

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

Adding new version

File size: 1.4 KB
Line 
1/*
2  String startWith() and endsWith()
3
4 Examples of how to use startsWith() and endsWith() in a String
5
6 created 27 July 2010
7 modified 2 Apr 2012
8 by Tom Igoe
9
10 http://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith
11
12 This example code is in the public domain.
13 */
14
15void setup() {
16  // Open serial communications and wait for port to open:
17  Serial.begin(9600);
18  while (!Serial) {
19    ; // wait for serial port to connect. Needed for native USB port only
20  }
21
22  // send an intro:
23  Serial.println("\n\nString startsWith() and endsWith():");
24  Serial.println();
25}
26
27void loop() {
28  // startsWith() checks to see if a String starts with a particular substring:
29  String stringOne = "HTTP/1.1 200 OK";
30  Serial.println(stringOne);
31  if (stringOne.startsWith("HTTP/1.1")) {
32    Serial.println("Server's using http version 1.1");
33  }
34
35  // you can also look for startsWith() at an offset position in the string:
36  stringOne = "HTTP/1.1 200 OK";
37  if (stringOne.startsWith("200 OK", 9)) {
38    Serial.println("Got an OK from the server");
39  }
40
41  // endsWith() checks to see if a String ends with a particular character:
42  String sensorReading = "sensor = ";
43  sensorReading += analogRead(A0);
44  Serial.print(sensorReading);
45  if (sensorReading.endsWith("0")) {
46    Serial.println(". This reading is divisible by ten");
47  } else {
48    Serial.println(". This reading is not divisible by ten");
49  }
50
51  // do nothing while true:
52  while (true);
53}
Note: See TracBrowser for help on using the repository browser.