Tutorial.StringAdditionOperator History

Hide minor edits - Show changes to output

August 27, 2015, at 01:01 PM by Simone Maiocchi -
Deleted line 0:
Changed line 90 from:
to:
(:div class=BOM:)
Changed line 92 from:
to:
(:divend:)
Changed lines 123-124 from:
* [[StringToIntExample|StringToInt]] - Allows you to convert a String to an integer number.
to:
* [[StringToInt]] - Allows you to convert a String to an integer number.
August 27, 2015, at 12:30 PM by Simone Maiocchi -
Changed lines 1-6 from:
(:div class=breadcrumb:)
[[Tutorial.HomePage | Tutorials]] > [[Tutorial.Examples | Examples]] > Strings > StringAdditionOperation
(:divend:)

(:*toc:)
to:

(:html:)

<div class="subHeader row">

<div class="breadcrumb columns large-10 medium-10">
<p>
<a class="wikilink" href="/en/Tutorial/HomePage">Tutorials </a>
> <a class="wikilink" href="/en/Tutorial/BuiltInExamples"> Built-In Examples </a> > 08.Strings > StringAdditionOperator
</p>
</div>
</div>

(:htmlend:)
Changed lines 111-123 from:
* [[Reference/StringObject|String object]]
* [[StringConstructors]]
* [[StringAdditionOperator]]
* [[StringIndexOf]]
* [[StringAppendOperator]]
* [[StringLengthTrim]]
* [[StringCaseChanges]]
* [[StringReplace]]
* [[StringCharacters]]
* [[StringStartsWithEndsWith]]
* [[StringComparisonOperators]]
* [[StringSubstring]]
to:
* [[Reference/StringObject|String object]] – Your Reference for String objects
* [[CharacterAnalysis]] - We use the operators that allow us to recognise the type of character we are dealing with.
* [[StringAppendOperator]] - Use the += operator and the concat() method to append things to Strings
* [[StringCaseChanges]] - Change the case of a string.
* [[StringCharacters]] - Get/set the value of a specific character in a string.
* [[StringComparisonOperators]] - Get/set the value of a specific character in a string.
* [[StringConstructors]] - Initialize string objects.
* [[StringIndexOf]] - Look for the first/last instance of a character in a string.
* [[StringLength]] - Get the length of a string.
* [[StringLengthTrim]] - Get and trim the length of a string.
* [[StringReplace]] - Replace individual characters in a string.
* [[StringStartsWithEndsWith]] - Check which characters/substrings a given string starts or ends with.
* [[StringSubstring]] - Look for "phrases" within a given string.
* [[StringToIntExample|StringToInt]] - Allows you to convert a String to an integer number.
July 30, 2015, at 01:41 PM by Simone Maiocchi -
Changed lines 87-88 from:
There is no circuit for this example, though your board must be connected to your computer via USB.
to:
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.
Changed lines 115-118 from:
to:
\\
''Last revision 2015/07/30 by SM ''

July 10, 2015, at 11:23 AM by Simone Maiocchi -
Changed line 101 from:
!!!See Also:
to:
!!!See Also
July 10, 2015, at 11:22 AM by Simone Maiocchi -
Changed lines 81-82 from:
!!!Hardware Required:
to:
!!!Hardware Required
Deleted lines 94-95:

(:div class=code:)
Added line 96:
(:div class=code:)
July 10, 2015, at 11:21 AM by Simone Maiocchi -
Changed lines 1-7 from:
''Examples > Strings''

!!!String Addition Operator

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.

(:div class=BOM :)
to:
(:div class=breadcrumb:)
[[Tutorial.HomePage | Tutorials]] &gt; [[Tutorial.Examples | Examples]] &gt; Strings &gt; StringAdditionOperation
Changed lines 5-7 from:
(:div class=code :)

(:source lang=arduino tabwidth=4:)
to:
(:*toc:)

!!String Addition Operator

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.


[@
Changed lines 27-29 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 31-32 from:
(:div class=code :)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 33-35 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 37-38 from:
(:div class=code :)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 39-41 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 46-47 from:
(:div class=code :)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 51-53 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 55-56 from:
(:div class=code :)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 59-61 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 65-66 from:
(:div class=code:)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 67-69 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 71-72 from:
(:div class=code:)
(:source lang=arduino tabwidth=4:)
to:
[@
Changed lines 75-77 from:
(:sourceend:)
(:divend:)
to:
@]
Changed lines 83-84 from:
* Arduino Board
to:
* Arduino or Genuino Board
Changed lines 87-88 from:
There is no circuit for this example, though your Arduino must be connected to your computer via USB.
to:
There is no circuit for this example, though your board must be connected to your computer via USB.
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 https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.ino lang=arduino tabwidth=4:)
to:
(:source https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples/8.Strings/StringAdditionOperator/StringAdditionOperator.pde lang=arduino tabwidth=4:)
to:
(:source https://raw.githubusercontent.com/arduino/Arduino/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 https://raw.githubusercontent.com/arduino/Arduino/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