Strings are the data type that is used to store a series of characters in it. In Arduino, strings are the same as in other programming languages. In various situations, strings need to be added in Arduino programming. We can add up the strings either by using the built-in function, concat() or we can use the addition operator, “+”, to add them. In this write-up, the addition of the strings is explained by using the addition operator.
How to use the addition operator to add the strings in Arduino
The use of the addition operator, “+”, for adding the strings in Arduino is similar to the concatenation process, it joins the two strings together. For example, we have two strings, one is “Linux” and the other is “Hint”, we can use the addition operator to join them and the result will be “LinuxHint”.
Is there any restriction of adding the strings with other data types using the addition operator
No, there is no restriction of adding the strings with other data types using the addition operator. We can add the numbers of integer and floating data types with strings using the addition operator. We can also add up the characters and the output of the millis() function to the strings. Moreover, we can add up more than one string using the addition operator in a single statement. We can understand all these by executing a simple Arduino code.
Example: Adding strings using the addition operator in Arduino
Consider the following simple example of simple Arduino code:
Serial.begin(9600);
s1= "Linux";
s2= "Hint ";
s3= s1+s2;
Serial.print("The addition of s1 and s2 is: ");
Serial.println(s3);
Serial.print("Now, we will do addition of characters with s1 and s2: ");
s3="Welcome to "+s1+s2;
Serial.println(s3);
Serial.print("We will do addition of integer data type with s1 and s2: ");
s3=s1+s2+100;
Serial.println(s3);
Serial.print("We will do addition of float data type with s1 and s2: ");
s3=s1+s2+0.50;
Serial.println(s3);
Serial.print("We will do addition of output of millis() function with s1 and s2: ");
s3=s1+s2+millis();
Serial.println(s3);
}
void loop() {
}
The output of the above executed Arduino code is:
Explanation: The code executed above is very simple, we declare three variables s1,s2, and s3 with String data type. In s1 and s2 we stored some strings, then first, added up both strings using the addition operator in s3 and displayed the results on the serial monitor through serial communication at a baud rate of 9600. Next, we added up the characters, integer data type, float data type, the output of millis() function with strings s1 and s2 and displayed each result on the serial monitor.
Conclusion
The strings can be added up in Arduino using the addition operator, this process is also known as concatenation. We can concatenate the two or more than two strings using the built-in function of concat() as well as using the addition operator. In this write-up, the addition of string using the addition operator is explained in Arduino with the help of an example.