Hide minor edits - Show changes to markup
Here's a working example of several different concatenation examples:
Here's a working example of several different concatenation examples :
(:div class=circuit :)
(:div class=circuit:)
(:div class=code :)
(:div class=code:)
(:divend:)
(:div class=code :)
(:div class=code:)
(:div class=code :)
(:div class=code:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/08.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
Here's a working example of several different concatenation examples:
There is no circuit for this example.
There is no circuit for this example, though your Arduino must be connected to your computer via USB.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
Here's a working example of several different concatenation examples:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
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.
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.
No external hardware is required.
[@
(:source lang=arduino tabwidth=4:)
@]
(:sourceend:)
[@
(:source lang=arduino tabwidth=4:)
@]
(:sourceend:)
stringThree = stringOne + analogRead(0);
(:source lang=arduino tabwidth=4:)
stringThree = stringOne + analogRead(A0);
(:sourceend:)
int sensorValue = analogRead(0);
int sensorValue = analogRead(A0);
[@
int sensorValue = analogRead(0);
(:source lang=arduino tabwidth=4:)
int sensorValue = analogRead(A0);
@]
(:sourceend:)
Serial.println("I want " + analogRead(0) + " donuts");
(:source lang=arduino tabwidth=4:)
Serial.println("I want " + analogRead(A0) + " donuts");
(:sourceend:)
[@
int sensorValue = analogRead(0);
(:source lang=arduino tabwidth=4:)
int sensorValue = analogRead(A0);
@]
(:sourceend:)
(:sourceend:)
[@
(:source lang=arduino tabwidth=4:)
@]
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
This is allowable since the @@millis() function returns a long integer, which can be added to a String. You could also do this:
This is allowable since the millis() function returns a long integer, which can be added to a String. You could also do this:
Examples > Control Structures
Examples > Strings
[@ int sensorValue = analogRead(0);
String stringOne = "Sensor value: ";
[@
int sensorValue = analogRead(0); String stringOne = "Sensor value: ";
[@ int sensorValue = analogRead(0);
[@
int sensorValue = analogRead(0);
Serial.println("I want " + analogRead(0) + " donuts");
Serial.println("I want " + analogRead(0) + " donuts");
[@
@]
[@
@]
(:div class=circuit :) There is no circuit for this example. (:divend:)
Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
int sensorValue = analogRead(0); String stringOne = "Sensor value: "; String stringThree = stringOne + sensorValue; Serial.println(stringThree);
results in "Sensor Value: 402" or whatever the analogRead() result is, but
(:div class=code :)
int sensorValue = analogRead(0); String stringThree = "Sensor value: " + sensorValue; Serial.println(stringThree);
(:divend:)
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:
(:div class=code :)
Serial.println("I want " + analogRead(0) + " 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:
(:div class=code :)
int sensorValue = analogRead(0); String stringThree = "I want " + sensorValue; Serial.println(stringThree + " donuts");
(:divend:)
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.
Here's a working example of several different concatenation examples:
(:div class=circuit :) There is no circuit for this example. (:divend:)
(:div class=code :)
(:source
Schematic:
(:div class=schematic :) There is no circuit for this example. (:divend:)
The String object 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.
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.
Examples > Control Structures
The String object 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.
(:div class=BOM :)
No external hardware is required. (:divend:)
(:div class=code :)
// 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;
(:divend:)
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,
(:div class=code :)
stringThree = stringOne + millis();
(:divend:)
This is allowable since the @@millis() function returns a long integer, which can be added to a String. You could also do this:
(:div class=code :)
stringThree = stringOne + analogRead(0);
(:divend:)
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.
(:div class=circuit :) There is no circuit for this example. (:divend:)
Schematic:
(:div class=schematic :) There is no circuit for this example. (:divend:)
(:div class=code :)
(:source