2010-07-27 21:03:52 +02:00
|
|
|
/*
|
|
|
|
String constructors
|
|
|
|
|
|
|
|
Examples of how to create strings from other data types
|
|
|
|
|
|
|
|
created 27 July 2010
|
|
|
|
by Tom Igoe
|
|
|
|
|
|
|
|
This example code is in the public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2010-08-11 22:24:10 +02:00
|
|
|
// using a constant String:
|
|
|
|
String stringOne = "Hello String";
|
2010-07-27 21:03:52 +02:00
|
|
|
Serial.println(stringOne); // prints "Hello String"
|
|
|
|
|
2010-08-11 22:24:10 +02:00
|
|
|
// converting a constant char into a String:
|
|
|
|
stringOne = String('a');
|
2010-07-27 21:03:52 +02:00
|
|
|
Serial.println(stringOne); // prints "a"
|
|
|
|
|
2010-08-11 22:24:10 +02:00
|
|
|
// converting a constant string into a String object:
|
|
|
|
String stringTwo = String("This is a string");
|
2010-07-27 21:03:52 +02:00
|
|
|
Serial.println(stringTwo); // prints "This is a string"
|
|
|
|
|
2010-08-11 22:24:10 +02:00
|
|
|
// concatenating two strings:
|
|
|
|
stringOne = String(stringTwo + " with more");
|
|
|
|
// prints "This is a string with more":
|
|
|
|
Serial.println(stringOne);
|
2010-07-27 21:03:52 +02:00
|
|
|
|
2010-08-11 22:24:10 +02:00
|
|
|
// using a constant integer:
|
|
|
|
stringOne = String(13);
|
2010-07-27 21:03:52 +02:00
|
|
|
Serial.println(stringOne); // prints "13"
|
|
|
|
|
2010-08-11 22:24:10 +02:00
|
|
|
// using an int and a base:
|
|
|
|
stringOne = String(analogRead(0), DEC);
|
|
|
|
// prints "453" or whatever the value of analogRead(0) is
|
|
|
|
Serial.println(stringOne);
|
|
|
|
|
|
|
|
// using an int and a base (hexadecimal):
|
|
|
|
stringOne = String(45, HEX);
|
|
|
|
// prints "2d", which is the hexadecimal version of decimal 45:
|
|
|
|
Serial.println(stringOne);
|
|
|
|
|
|
|
|
// using an int and a base (binary)
|
|
|
|
stringOne = String(255, BIN);
|
|
|
|
// prints "11111111" which is the binary value of 255
|
|
|
|
Serial.println(stringOne);
|
|
|
|
|
|
|
|
// using a long and a base:
|
|
|
|
stringOne = String(millis(), DEC);
|
|
|
|
// prints "123456" or whatever the value of millis() is:
|
|
|
|
Serial.println(stringOne);
|
2010-07-27 21:03:52 +02:00
|
|
|
|
|
|
|
// do nothing while true:
|
|
|
|
while(true);
|
|
|
|
|
2010-08-01 16:29:01 +02:00
|
|
|
}
|