2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
String startWith() and endsWith()
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
Examples of how to use startsWith() and endsWith() in a String
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
created 27 July 2010
|
2012-04-02 15:07:58 +02:00
|
|
|
modified 2 Apr 2012
|
2011-08-30 21:33:32 +02:00
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
http://arduino.cc/en/Tutorial/StringStartsWithEndsWith
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
This example code is in the public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void setup() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2011-08-30 21:33:32 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-06 20:00:31 +02:00
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2012-04-06 20:00:31 +02:00
|
|
|
// send an intro:
|
2011-08-30 21:33:32 +02:00
|
|
|
Serial.println("\n\nString startsWith() and endsWith():");
|
2012-04-06 20:00:31 +02:00
|
|
|
Serial.println();
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2012-04-06 12:59:54 +02:00
|
|
|
// startsWith() checks to see if a String starts with a particular substring:
|
|
|
|
String stringOne = "HTTP/1.1 200 OK";
|
|
|
|
Serial.println(stringOne);
|
2011-08-30 21:33:32 +02:00
|
|
|
if (stringOne.startsWith("HTTP/1.1")) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println("Server's using http version 1.1");
|
|
|
|
}
|
2012-04-06 12:59:54 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
// you can also look for startsWith() at an offset position in the string:
|
2012-04-06 12:59:54 +02:00
|
|
|
stringOne = "HTTP/1.1 200 OK";
|
2011-08-30 21:33:32 +02:00
|
|
|
if (stringOne.startsWith("200 OK", 9)) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println("Got an OK from the server");
|
|
|
|
}
|
2012-04-06 12:59:54 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
// endsWith() checks to see if a String ends with a particular character:
|
|
|
|
String sensorReading = "sensor = ";
|
|
|
|
sensorReading += analogRead(A0);
|
|
|
|
Serial.print (sensorReading);
|
2013-11-18 10:27:36 +01:00
|
|
|
if (sensorReading.endsWith("0")) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(". This reading is divisible by ten");
|
|
|
|
}
|
2011-08-30 21:33:32 +02:00
|
|
|
else {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(". This reading is not divisible by ten");
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
2012-04-06 12:59:54 +02:00
|
|
|
// do nothing while true:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2012-04-06 12:59:54 +02:00
|
|
|
}
|