2010-07-27 21:03:52 +02:00
|
|
|
/*
|
|
|
|
Adding Strings together
|
|
|
|
|
|
|
|
Examples of how to add strings together
|
|
|
|
You can also add several different data types to string, as shown here:
|
|
|
|
|
|
|
|
created 27 July 2010
|
2010-09-04 21:47:59 +02:00
|
|
|
modified 4 Sep 2010
|
2010-07-27 21:03:52 +02:00
|
|
|
by Tom Igoe
|
|
|
|
|
2010-08-11 23:56:28 +02:00
|
|
|
http://arduino.cc/en/Tutorial/StringAdditionOperator
|
|
|
|
|
2010-07-28 00:59:18 +02:00
|
|
|
This example code is in the public domain.
|
2010-07-27 21:03:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// declare three strings:
|
|
|
|
String stringOne, stringTwo, stringThree;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
stringOne = String("stringThree = ");
|
|
|
|
stringTwo = String("this string");
|
|
|
|
stringThree = String ();
|
|
|
|
Serial.println("\n\nAdding strings together (concatenation):");
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// adding a constant integer to a string:
|
|
|
|
stringThree = stringOne + 123;
|
2010-08-11 22:26:10 +02:00
|
|
|
Serial.println(stringThree); // prints "stringThree = 123"
|
2010-07-27 21:03:52 +02:00
|
|
|
|
|
|
|
// adding a constant long interger to a string:
|
|
|
|
stringThree = stringOne + 123456789;
|
|
|
|
Serial.println(stringThree); // prints " You added 123456789"
|
|
|
|
|
|
|
|
// adding a constant character to a string:
|
|
|
|
stringThree = stringOne + 'A';
|
|
|
|
Serial.println(stringThree); // prints "You added A"
|
|
|
|
|
|
|
|
// adding a constant string to a string:
|
|
|
|
stringThree = stringOne + "abc";
|
|
|
|
Serial.println(stringThree); // prints "You added abc"
|
|
|
|
|
|
|
|
stringThree = stringOne + stringTwo;
|
|
|
|
Serial.println(stringThree); // prints "You added this string"
|
|
|
|
|
|
|
|
// adding a variable integer to a string:
|
2010-09-04 21:47:59 +02:00
|
|
|
int sensorValue = analogRead(A0);
|
2010-07-27 21:03:52 +02:00
|
|
|
stringOne = "Sensor value: ";
|
|
|
|
stringThree = stringOne + sensorValue;
|
2010-09-04 21:47:59 +02:00
|
|
|
Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
|
2010-07-27 21:03:52 +02:00
|
|
|
|
|
|
|
// adding a variable long integer to a string:
|
|
|
|
long currentTime = millis();
|
|
|
|
stringOne="millis() value: ";
|
|
|
|
stringThree = stringOne + millis();
|
|
|
|
Serial.println(stringThree); // prints "The millis: 345345" or whatever value currentTime has
|
|
|
|
|
|
|
|
// do nothing while true:
|
|
|
|
while(true);
|
2010-08-01 16:29:01 +02:00
|
|
|
}
|