2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
Adding Strings together
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Examples of how to add Strings together
|
|
|
|
You can also add several different data types to String, as shown here:
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 27 Jul 2010
|
2017-07-15 00:30:34 +02:00
|
|
|
modified 2 Apr 2012
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
This example code is in the public domain.
|
2017-07-12 22:18:23 +02:00
|
|
|
|
|
|
|
http://www.arduino.cc/en/Tutorial/StringAdditionOperator
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// declare three Strings:
|
2011-08-30 21:33:32 +02:00
|
|
|
String stringOne, stringTwo, stringThree;
|
|
|
|
|
|
|
|
void setup() {
|
2012-04-02 15:07:58 +02:00
|
|
|
// initialize serial and wait for port to open:
|
2011-08-30 21:33:32 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-06 20:00:31 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-06 20:00:31 +02:00
|
|
|
}
|
2012-04-06 12:59:54 +02:00
|
|
|
|
2016-02-25 18:39:55 +01:00
|
|
|
stringOne = String("You added ");
|
2011-08-30 21:33:32 +02:00
|
|
|
stringTwo = String("this string");
|
2015-07-06 15:18:33 +02:00
|
|
|
stringThree = String();
|
2012-04-06 20:00:31 +02:00
|
|
|
// send an intro:
|
2017-07-12 19:58:31 +02:00
|
|
|
Serial.println("\n\nAdding Strings together (concatenation):");
|
2012-04-06 20:00:31 +02:00
|
|
|
Serial.println();
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a constant integer to a String:
|
2011-08-30 21:33:32 +02:00
|
|
|
stringThree = stringOne + 123;
|
2016-02-25 18:39:55 +01:00
|
|
|
Serial.println(stringThree); // prints "You added 123"
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a constant long integer to a String:
|
2011-08-30 21:33:32 +02:00
|
|
|
stringThree = stringOne + 123456789;
|
2016-02-25 18:39:55 +01:00
|
|
|
Serial.println(stringThree); // prints "You added 123456789"
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a constant character to a String:
|
2011-08-30 21:33:32 +02:00
|
|
|
stringThree = stringOne + 'A';
|
|
|
|
Serial.println(stringThree); // prints "You added A"
|
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a constant string to a String:
|
2011-08-30 21:33:32 +02:00
|
|
|
stringThree = stringOne + "abc";
|
|
|
|
Serial.println(stringThree); // prints "You added abc"
|
|
|
|
|
|
|
|
stringThree = stringOne + stringTwo;
|
|
|
|
Serial.println(stringThree); // prints "You added this string"
|
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a variable integer to a String:
|
2011-08-30 21:33:32 +02:00
|
|
|
int sensorValue = analogRead(A0);
|
|
|
|
stringOne = "Sensor value: ";
|
|
|
|
stringThree = stringOne + sensorValue;
|
|
|
|
Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
|
|
|
|
|
2017-07-12 19:58:31 +02:00
|
|
|
// adding a variable long integer to a String:
|
2013-10-21 09:58:40 +02:00
|
|
|
stringOne = "millis() value: ";
|
2011-08-30 21:33:32 +02:00
|
|
|
stringThree = stringOne + millis();
|
2017-07-12 19:58:31 +02:00
|
|
|
Serial.println(stringThree); // prints "The millis: 345345" or whatever value millis() has
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
// do nothing while true:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2012-04-06 12:59:54 +02:00
|
|
|
}
|