| 1 |
/* |
| 2 |
String substring() |
| 3 |
|
| 4 |
Examples of how to use substring in a String |
| 5 |
|
| 6 |
created 27 Jul 2010, |
| 7 |
modified 2 Apr 2012 |
| 8 |
by Zach Eveland |
| 9 |
|
| 10 |
This example code is in the public domain. |
| 11 |
|
| 12 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringSubstring |
| 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 substring():"); |
| 24 |
Serial.println(); |
| 25 |
} |
| 26 |
|
| 27 |
void loop() { |
| 28 |
// Set up a String: |
| 29 |
String stringOne = "Content-Type: text/html"; |
| 30 |
Serial.println(stringOne); |
| 31 |
|
| 32 |
// substring(index) looks for the substring from the index position to the end: |
| 33 |
if (stringOne.substring(19) == "html") { |
| 34 |
Serial.println("It's an html file"); |
| 35 |
} |
| 36 |
// you can also look for a substring in the middle of a string: |
| 37 |
if (stringOne.substring(14, 18) == "text") { |
| 38 |
Serial.println("It's a text-based file"); |
| 39 |
} |
| 40 |
|
| 41 |
// do nothing while true: |
| 42 |
while (true); |
| 43 |
} |