Arduino示例代码讲解:String replace() 字符串替换
Arduino示例代码讲解:String replace字符串替换
- String replace() 字符串替换
-
- 代码功能概述
-
-
- 功能:
- 硬件要求:
- 输出:
-
- 代码结构
-
-
- `setup()` 函数
- `loop()` 函数
- 替换子字符串:
- 替换单个字符:
- 无限循环:
-
- 运行过程
- 输出结果解释
-
-
- 原始字符串:
- 替换后的字符串:
- 原始字符串:
- 替换后的字符串:
-
- 注意事项
String replace() 字符串替换
/*
String replace()
Examples of how to replace characters or substrings of a string
created 27 July 2010
modified 2 Apr 2012
by Tom Igoe
http://arduino.cc/en/Tutorial/StringReplace
This example code is in the public domain.
*/
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// send an intro:
Serial.println("\n\nString replace:\n");
Serial.println();
}
void loop() {
String stringOne = "<html><head><body>";
Serial.println(stringOne);
// replace() changes all instances of one substring with another:
// first, make a copy of th original string:
String stringTwo = stringOne;
// then perform the replacements:
stringTwo.replace("<", "</");