Tutorial.StringAdditionOperator History

Hide minor edits - Show changes to output

May 02, 2012, at 04:03 PM by Scott Fitzgerald -
Added line 108:
Changed lines 110-111 from:
Here's a working example of several different concatenation examples:
to:
Here's a working example of several different concatenation examples :
May 02, 2012, at 04:03 PM by Scott Fitzgerald -
Changed line 99 from:
(:div class=circuit :)
to:
(:div class=circuit:)
Deleted line 108:
Added line 110:
May 02, 2012, at 04:02 PM by Scott Fitzgerald -
Changed line 74 from:
(:div class=code :)
to:
(:div class=code:)
Changed lines 78-79 from:
to:
(:divend:)
Changed line 82 from:
(:div class=code :)
to:
(:div class=code:)
Changed lines 108-109 from:
(:div class=code :)
to:
(:div class=code:)
May 02, 2012, at 04:01 PM by Scott Fitzgerald -
Changed line 110 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/08.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:25 AM by Scott Fitzgerald -
Changed line 110 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
September 23, 2010, at 10:44 PM by Christian Cerrito -
Changed lines 3-4 from:
!!String Addition Operator
to:
!!!String Addition Operator
September 19, 2010, at 11:04 PM by Christian Cerrito -
Added lines 127-128:

September 19, 2010, at 10:43 PM by Christian Cerrito -
Changed lines 110-111 from:

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
September 19, 2010, at 10:43 PM by Christian Cerrito -
Changed lines 93-94 from:
Here's a working example of several different concatenation examples:
to:
!!!Hardware Required:

* Arduino Board
Deleted line 97:
Changed lines 99-103 from:
There is no circuit for this example.
to:
There is no circuit for this example, though your Arduino must be connected to your computer via USB.

%height=250px%[[Attach:Arduino_bb.png | Attach:Arduino_bb.png]]
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]
Changed lines 108-109 from:

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:

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:)
September 19, 2010, at 10:22 PM by Christian Cerrito -
Changed lines 5-6 from:
You can add [[Reference/StringObject|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.
to:
You can add [[Reference/StringObject|'''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.
September 19, 2010, at 10:22 PM by Christian Cerrito -
Deleted lines 7-8:
!!!Hardware Required
No external hardware is required.
September 19, 2010, at 10:04 PM by Christian Cerrito -
Changed lines 3-4 from:
!!!String Addition Operator
to:
!!String Addition Operator
September 17, 2010, at 12:01 AM by Tom Igoe -
September 16, 2010, at 10:28 PM by Tom Igoe -
Changed line 14 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed line 29 from:
@]
to:
(:sourceend:)
Changed line 35 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed line 37 from:
@]
to:
(:sourceend:)
Changed lines 43-45 from:
[@
stringThree = stringOne + analogRead(0);
@]
to:
(:source lang=arduino tabwidth=4:)
stringThree = stringOne + analogRead(A0);
(:sourceend:)
Changed line 55 from:
int sensorValue = analogRead(0);
to:
int sensorValue = analogRead(A0);
Changed lines 65-66 from:
[@
int sensorValue = analogRead(0);
to:
(:source lang=arduino tabwidth=4:)
int sensorValue = analogRead(A0);
Changed line 69 from:
@]
to:
(:sourceend:)
Changed lines 77-80 from:
[@
Serial.println("I want " + analogRead(0) + " donuts");
@]
to:
(:source lang=arduino tabwidth=4:)
Serial.println("I want " + analogRead(A0) + " donuts");
(:sourceend:)
Changed lines 84-85 from:
[@
int sensorValue = analogRead(0);
to:
(:source lang=arduino tabwidth=4:)
int sensorValue = analogRead(A0);
Changed line 88 from:
@]
to:
(:sourceend:)
September 16, 2010, at 10:26 PM by Tom Igoe -
September 16, 2010, at 10:25 PM by Tom Igoe -
Changed line 59 from:
to:
(:sourceend:)
September 16, 2010, at 10:22 PM by Tom Igoe -
Changed line 54 from:
[@
to:
(:source lang=arduino tabwidth=4:)
Changed line 59 from:
@]
to:
Changed line 106 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
August 15, 2010, at 03:25 PM by Tom Igoe -
Changed lines 40-41 from:
This is allowable since the @@millis() function returns a long integer, which can be added to a String. You could also do this:
to:
This is allowable since the @@millis()@@ function returns a long integer, which can be added to a String. You could also do this:
August 01, 2010, at 04:11 PM by Tom Igoe -
August 01, 2010, at 04:10 PM by Tom Igoe -
Changed lines 1-2 from:
''Examples > Control Structures''
to:
''Examples > Strings''
August 01, 2010, at 03:35 PM by Tom Igoe -
Changed lines 54-55 from:
[@ int sensorValue = analogRead(0);
String stringOne = "Sensor value: ";
to:
[@
int sensorValue = analogRead(0);
String stringOne = "Sensor value: ";
Changed lines 65-66 from:
[@ int sensorValue = analogRead(0);
to:
[@
int sensorValue = analogRead(0);
Changed line 78 from:
Serial.println("I want " + analogRead(0) + " donuts");
to:
Serial.println("I want " + analogRead(0) + " donuts");
August 01, 2010, at 03:34 PM by Tom Igoe -
Added line 35:
[@
Added line 37:
@]
Added line 43:
[@
Added line 45:
@]
Changed lines 50-56 from:
!!!Circuit

(:div class=circuit :)
There is no circuit for this example.
(:divend:)

!!!Code
to:
'''Caution:'''
You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:
Changed lines 54-56 from:

(:source http://arduino.cc/en/pub/code/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
[@ int sensorValue = analogRead(0);
String stringOne = "Sensor value: ";
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);
@]
Added lines 61-107:
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:

!!!Circuit

(:div class=circuit :)
There is no circuit for this example.
(:divend:)

!!!Code
(:div class=code :)

(:source
August 01, 2010, at 03:09 PM by Tom Igoe -
Deleted lines 51-57:
'''Schematic:'''

(:div class=schematic :)
There is no circuit for this example.
(:divend:)

August 01, 2010, at 02:52 PM by Tom Igoe -
Changed lines 5-6 from:
The [[Reference/StringObject|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.
to:
You can add [[Reference/StringObject|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.
August 01, 2010, at 02:50 PM by Tom Igoe -
Changed lines 3-4 from:
!!!String Addition Operators
to:
!!!String Addition Operator
August 01, 2010, at 02:49 PM by Tom Igoe -
Changed line 68 from:
* [[StringConstuctors]]
to:
* [[StringConstructors]]
August 01, 2010, at 02:49 PM by Tom Igoe -
Added lines 1-78:
''Examples > Control Structures''

!!!String Addition Operators

The [[Reference/StringObject|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 :)
!!!Hardware Required
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.

!!!Circuit

(:div class=circuit :)
There is no circuit for this example.
(:divend:)

'''Schematic:'''

(:div class=schematic :)
There is no circuit for this example.
(:divend:)


!!!Code
(:div class=code :)

(:source

Share