| 1 |
/* |
| 2 |
String startWith() and endsWith() |
| 3 |
|
| 4 |
Examples of how to use startsWith() and endsWith() in a String |
| 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/StringStartsWithEndsWith |
| 13 |
*/ |
| 14 |
|
| 15 |
void 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 |
|
| 27 |
void 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 |
} |