| 1 |
/* |
| 2 |
String replace() |
| 3 |
|
| 4 |
Examples of how to replace characters or substrings of 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/StringReplace |
| 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 replace:\n"); |
| 24 |
Serial.println(); |
| 25 |
} |
| 26 |
|
| 27 |
void loop() { |
| 28 |
String stringOne = "<html><head><body>"; |
| 29 |
Serial.println(stringOne); |
| 30 |
// replace() changes all instances of one substring with another: |
| 31 |
// first, make a copy of the original string: |
| 32 |
String stringTwo = stringOne; |
| 33 |
// then perform the replacements: |
| 34 |
stringTwo.replace("<", "</"); |
| 35 |
// print the original: |
| 36 |
Serial.println("Original string: " + stringOne); |
| 37 |
// and print the modified string: |
| 38 |
Serial.println("Modified string: " + stringTwo); |
| 39 |
|
| 40 |
// you can also use replace() on single characters: |
| 41 |
String normalString = "bookkeeper"; |
| 42 |
Serial.println("normal: " + normalString); |
| 43 |
String leetString = normalString; |
| 44 |
leetString.replace('o', '0'); |
| 45 |
leetString.replace('e', '3'); |
| 46 |
Serial.println("l33tspeak: " + leetString); |
| 47 |
|
| 48 |
// do nothing while true: |
| 49 |
while (true); |
| 50 |
} |