Arduino示例代码讲解:String constructors 字符串构造器
String constructors 字符串构造器
这段代码是一个Arduino示例程序,用于演示如何使用String
类的构造函数从其他数据类型创建字符串。
/*
String constructors
Examples of how to create strings from other data types
created 27 July 2010
modified 30 Aug 2011
by Tom Igoe
http://arduino.cc/en/Tutorial/StringConstructors
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 Constructors:");
Serial.println();
}
void loop() {
// using a constant String:
String stringOne = "Hello String";
Serial.println(stringOne); // prints "Hello String"
// converting a constant char into a String:
stringOne = String('a');
Serial.println(stringOne); // prints "a"
// converting a constant string into a String object:
String stringTwo = String("This is a string");
Serial.println(stringTwo); // prints "This is a string"
// concatenating two strings:
stringOne = String(stringTwo