Debian Codegraph @

examples/08.Strings/StringAppendOperator/StringAppendOperator.ino

raw
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 Jul 2010
7 modified 2 Apr 2012
8 by Tom Igoe
9
10 This example code is in the public domain.
11
12 https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringAppendOperator
13 */
14
15 String stringOne, stringTwo;
16
17 void 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
31 void 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 }