## String Addition Operator
You can add [Strings](https://www.arduino.cc/en/Reference/StringObject) 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.
// adding a constant integer to a string:
stringThree = stringOne + 123;
// adding a constant long interger to a string:
stringThree = stringOne + 123456789;
// adding a constant character to a string:
stringThree = stringOne + 'A';
// adding a constant string to a string:
stringThree = stringOne + "abc";
// adding two Strings together:
stringThree = 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,
stringThree = 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:
stringThree = 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:
int sensorValue = analogRead(A0);
String stringOne = "Sensor value: ";
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);
results in "Sensor Value: 402" or whatever the `analogRead()` result is, but
int sensorValue = analogRead(A0);
String stringThree = "Sensor value: " + sensorValue;
Serial.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:
Serial.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:
int sensorValue = analogRead(A0);
String stringThree = "I want " + sensorValue;
Serial.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 or Genuino 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.
[![](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png)](https://www.arduino.cc/en/uploads/Tutorial/Arduino_bb.png)
image developed using [Fritzing](http://www.fritzing.org/). For more circuit examples, see the [Fritzing project page](http://fritzing.org/projects/)
### Code
Here's a working example of several different concatenation examples :
/*
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 Jul 2010
modified 2 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/StringAdditionOperator
*/
// declare three Strings:
String stringOne, stringTwo, stringThree;
void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
stringOne = String("You added ");
stringTwo = String("this string");
stringThree = String();
// send an intro:
Serial.println("\n\nAdding Strings together (concatenation):");
Serial.println();
}
void loop() {
// adding a constant integer to a String:
stringThree = stringOne + 123;
Serial.println(stringThree); // prints "You added 123"
// adding a constant long integer 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:
int sensorValue = analogRead(A0);
stringOne = "Sensor value: ";
stringThree = stringOne + sensorValue;
Serial.println(stringThree); // prints "Sensor Value: 401" or whatever value analogRead(A0) has
// adding a variable long integer to a String:
stringOne = "millis() value: ";
stringThree = stringOne + millis();
Serial.println(stringThree); // prints "The millis: 345345" or whatever value millis() has
// do nothing while true:
while (true);
}
[[Get Code]](https://www.arduino.cc/en/Tutorial/StringAdditionOperator?action=sourceblock&num=1)
### See Also
* [String object](https://www.arduino.cc/en/Reference/StringObject) – Your Reference for String objects
* [CharacterAnalysis](https://www.arduino.cc/en/Tutorial/CharacterAnalysis) - We use the operators that allow us to recognise the type of character we are dealing with.
* [StringAppendOperator](https://www.arduino.cc/en/Tutorial/StringAppendOperator) - Use the += operator and the concat() method to append things to Strings
* [StringCaseChanges](https://www.arduino.cc/en/Tutorial/StringCaseChanges) - Change the case of a string.
* [StringCharacters](https://www.arduino.cc/en/Tutorial/StringCharacters) - Get/set the value of a specific character in a string.
* [StringComparisonOperators](https://www.arduino.cc/en/Tutorial/StringComparisonOperators) - Get/set the value of a specific character in a string.
* [StringConstructors](https://www.arduino.cc/en/Tutorial/StringConstructors) - Initialize string objects.
* [StringIndexOf](https://www.arduino.cc/en/Tutorial/StringIndexOf) - Look for the first/last instance of a character in a string.
* [StringLength](https://www.arduino.cc/en/Tutorial/StringLength) - Get the length of a string.
* [StringLengthTrim](https://www.arduino.cc/en/Tutorial/StringLengthTrim) - Get and trim the length of a string.
* [StringReplace](https://www.arduino.cc/en/Tutorial/StringReplace) - Replace individual characters in a string.
* [StringStartsWithEndsWith](https://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith) - Check which characters/substrings a given string starts or ends with.
* [StringSubstring](https://www.arduino.cc/en/Tutorial/StringSubstring) - Look for "phrases" within a given string.
* [StringToInt](https://www.arduino.cc/en/Tutorial/StringToInt) - Allows you to convert a String to an integer number.
- 说明
- 系统示例文件目录结构及说明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (与特定硬件相关,暂无)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暂无)
- ArduinoISP