Arduino

Concatenate Strings in Arduino

While programming Arduino boards strings are frequently used inside Arduino sketches. Strings are like sentences that store information as an array of characters. They are useful for communicating with users and less useful when information is stored inside them for computers to use. This article will cover how more than one string can be concatenated using different ways.

Concatenate Strings in Arduino

Concatenation of strings means joining two or more strings containing a list of characters together into a single string. Concatenation is quite simple in Arduino just like we do in C++, there are two possible ways of concatenating strings in Arduino IDE.

Ways to Concatenate Strings in Arduino

Following are the two frequently used ways of concatenating strings in Arduino:

    • Using concat() Function
    • Using Append “+” Operator

1. Using concat() Function

The concat() function can append the given parameters with a string. This function can also return true if the concatenation operation is successful otherwise it will return false.

Syntax

Syntax of concat() function:

myString.concat(parameter)

 
Here in above syntax myString is the variable where a string is stored. While parameter is the variable, we want to append inside the myString. The parameter can contain any data type such as float, char, byte, long, int, double, and string.

Once the concat() function is executed myString will be changed because the given parameter will now be attached to it and the new result will be saved inside myString variable.

Parameters

It takes following parameters:

    • myString: a variable whose data type is String.
    • parameter: A variable with allowed data types: String, string, char, byte, int, unsigned int, long, unsigned long, float, double.

Return Value

This function can return two values that are:

true: When string is appended successfully.

false: When string remains unchanged.

Example Program

 

void setup() {
  Serial.begin(9600); /*Serial Communication Begins*/
Serial.print("myString before concat = ");
  String myString = "Linux";  /*myString is initialized*/
  Serial.println(myString); /*myString before concat*/
  myString.concat("hint.com");  /*concat Function called*/
Serial.print("myString after concat = ");
  Serial.println(myString); /*myString after concat*/
}
void loop() {
}

 
This code takes the two strings and amends them together using concat() function. myString variable is defined which takes the parameters value from concat() function argument and prints it on serial monitor.

Output

The output shows us myString before and after the concat() function.

2. Using Append Operator “+” in Arduino

Second way of doing the concatenation of strings is using the append operator “+”. Using append operator + we can concatenate strings with variables and the allowed data types for the variable is the same as concat() function. To concatenate multiple strings or variables in a single line we can use the append operator multiple times.

Syntax

Following is the syntax used for concatenation using the append operator +.

myString = parameter1 + parameter2 + ... + parameter_n;

 
Here we can concatenate any number of parameters and store the output in myString function. Allowed data types for parameters are int, double, float, double, char, byte, and string.

Parameters

It can take an infinite number of parameters.

    • myString1: Variable which is String
    • myString2: Variable which is String
    • myStringN: Variable which is String

Return

A new string will be returned which is the result of a combination of two different strings.

Example Program

 

void setup() {
   Serial.begin(9600);   /*Serial Communication Begins*/
   String s1 = "Linux";  /*String s1 is Defined*/
   String s2 = "hint.com "; /*String s2 is Defined*/
   String s3 = s1 + s2;  
   Serial.println(s3);  /*s1+s2 concatenated using + Operator*/
   s3 = s1 + s2 + 1;
   Serial.println(s3);  /*An int is concatenated using + Operator*/
   s3 = s1 + s2 + 'A' ;
   Serial.println(s3);  /*A CHAR is concatenated using + Operator*/
   s3 = s1 + s2+ 1.1;
   Serial.println(s3);  /*Floating point value concatenated using + Operator*/
   s3 = "You are at " + s1 + s2;
   Serial.println(s3);  /*Three strings are concatenated using + Operator*/
}
void loop() {
}

 
In above code two strings are initialized next using append operator “+” we concatenate two strings s1 and s2 and stored their output inside a new string s3.

Next an int, char and a floating-point value is initialized and appended using the “+” operator. All these outputs are printed on a serial monitor.

Output

In the output window all strings are printed one by one after concatenating them using the append operator “+”.

Conclusion

In Arduino programming strings are concatenated using two different ways both these are inherited from C++ programming. First method used to concatenate strings is using the concat() function in Arduino code while the second is simpler by using the append operator “+”. Using the append operator any number of strings can be added.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.