String Addition Operator

Add strings together in a variety of ways.

You can add Strings together in a variety of ways. This is called concatenation and it results in the original String being longer by the length of the String or character array with which you concatenate it. The

+
operator allows you to combine a String with another String, with a constant character array, an ASCII representation of a constant or variable number, or a constant character.

1// adding a constant integer to a string:
2stringThree = stringOne + 123;
3// adding a constant long integer to a string:
4stringThree = stringOne + 123456789;
5// adding a constant character to a string:
6stringThree = stringOne + 'A';
7// adding a constant string to a string:
8stringThree = stringOne + "abc";
9// adding two Strings together:
10stringThree = stringOne + stringTwo;

You can also use the

+
operator to add the results of a function to a String, if the function returns one of the allowed data types mentioned above. For example,

1stringThree = stringOne + millis();

This is allowable since the

millis()
function returns a long integer, which can be added to a String. You could also do this:

1stringThree = stringOne + analogRead(A0);

because

analogRead()
returns an integer. String concatenation can be very useful when you need to display a combination of values and the descriptions of those values into one String to display via serial communication, on an LCD display, over an Ethernet connection, or anywhere that Strings are useful.

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

1int sensorValue = analogRead(A0);
2String stringOne = "Sensor value: ";
3String stringThree = stringOne + sensorValue;
4Serial.println(stringThree);

results in "Sensor Value: 402" or whatever the

analogRead()
result is, but

1int sensorValue = analogRead(A0);
2String stringThree = "Sensor value: " + sensorValue;
3Serial.println(stringThree);

gives unpredictable results because

stringThree
never got an initial value before you started concatenating different data types.

Here's another example where improper initialization will cause errors:

1Serial.println("I want " + analogRead(A0) + " donuts");

This won't compile because the compiler doesn't handle the operator precedence correctly. On the other hand, the following will compile, but it won't run as expected:

1int sensorValue = analogRead(A0);
2String stringThree = "I want " + sensorValue;
3Serial.println(stringThree + " donuts");

It doesn't run correctly for the same reason as before:

stringThree
never got an initial value before you started concatenating different data types.

For best results, initialize your Strings before you concatenate them.

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.

circuit

Code

Here's a working example of several different concatenation examples :

1/*
2
3 Adding Strings together
4
5 Examples of how to add Strings together
6
7 You can also add several different data types to String, as shown here:
8
9 created 27 Jul 2010
10
11 modified 2 Apr 2012
12
13 by Tom Igoe
14
15 This example code is in the public domain.
16
17 http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/StringAdditionOperator
18
19*/
20
21// declare three Strings:
22
23String stringOne, stringTwo, stringThree;
24
25void setup() {
26
27 // initialize serial and wait for port to open:
28
29 Serial.begin(9600);
30
31 while (!Serial) {
32
33 ; // wait for serial port to connect. Needed for native USB port only
34
35 }
36
37 stringOne = String("You added ");
38
39 stringTwo = String("this string");
40
41 stringThree = String();
42
43 // send an intro:
44
45 Serial.println("\n\nAdding Strings together (concatenation):");
46
47 Serial.println();
48}
49
50void loop() {
51
52 // adding a constant integer to a String:
53
54 stringThree = stringOne + 123;
55
56 Serial.println(stringThree); // prints "You added 123"
57
58 // adding a constant long integer to a String:
59
60 stringThree = stringOne + 123456789;
61
62 Serial.println(stringThree); // prints "You added 123456789"
63
64 // adding a constant character to a String:
65
66 stringThree = stringOne + 'A';
67
68 Serial.println(stringThree); // prints "You added A"
69
70 // adding a constant string to a String:
71
72 stringThree = stringOne + "abc";
73
74 Serial.println(stringThree); // prints "You added abc"
75
76 stringThree = stringOne + stringTwo;
77
78 Serial.println(stringThree); // prints "You added this string"
79
80 // adding a variable integer to a String:
81
82 int sensorValue = analogRead(A0);
83
84 stringOne = "Sensor value: ";
85
86 stringThree = stringOne + sensorValue;
87
88 Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
89
90 // adding a variable long integer to a String:
91
92 stringOne = "millis() value: ";
93
94 stringThree = stringOne + millis();
95
96 Serial.println(stringThree); // prints "The millis: 345345" or whatever value millis() has
97
98 // do nothing while true:
99
100 while (true);
101}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/07/30 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.