1/* 2 Appending to Strings using the += operator and concat() 3 4 Examples of how to append different data types to strings 5 6 created 27 July 2010 7 modified 2 Apr 2012 8 by Tom Igoe 9 10 http://www.arduino.cc/en/Tutorial/StringAppendOperator 11 12 This example code is in the public domain. 13 */ 14 15String stringOne, stringTwo; 16 17void setup() { 18 // Open serial communications and wait for port to open: 19 Serial.begin(9600); 20 while (!Serial) { 21 ; // wait for serial port to connect. Needed for native USB port only 22 } 23 24 stringOne = String("Sensor "); 25 stringTwo = String("value"); 26 // send an intro: 27 Serial.println("\n\nAppending to a string:"); 28 Serial.println(); 29} 30 31void loop() { 32 Serial.println(stringOne); // prints "Sensor " 33 34 // adding a string to a string: 35 stringOne += stringTwo; 36 Serial.println(stringOne); // prints "Sensor value" 37 38 // adding a constant string to a string: 39 stringOne += " for input "; 40 Serial.println(stringOne); // prints "Sensor value for input" 41 42 // adding a constant character to a string: 43 stringOne += 'A'; 44 Serial.println(stringOne); // prints "Sensor value for input A" 45 46 // adding a constant integer to a string: 47 stringOne += 0; 48 Serial.println(stringOne); // prints "Sensor value for input A0" 49 50 // adding a constant string to a string: 51 stringOne += ": "; 52 Serial.println(stringOne); // prints "Sensor value for input" 53 54 // adding a variable integer to a string: 55 stringOne += analogRead(A0); 56 Serial.println(stringOne); // prints "Sensor value for input A0: 456" or whatever analogRead(A0) is 57 58 Serial.println("\n\nchanging the Strings' values"); 59 stringOne = "A long integer: "; 60 stringTwo = "The millis(): "; 61 62 // adding a constant long integer to a string: 63 stringOne += 123456789; 64 Serial.println(stringOne); // prints "A long integer: 123456789" 65 66 // using concat() to add a long variable to a string: 67 stringTwo.concat(millis()); 68 Serial.println(stringTwo); // prints "The millis(): 43534" or whatever the value of the millis() is 69 70 // do nothing while true: 71 while (true); 72} 73 74