Concatenate Strings in Arduino

Ammar Ali Jan 30, 2023 Jun 14, 2022
  1. Concatenate Strings Using the concat() Function in Arduino
  2. Concatenate Strings Using the Append Operator + in Arduino
Concatenate Strings in Arduino

This tutorial will discuss concatenating two strings using the concat() function or the append operator in Arduino.

Concatenate Strings Using the concat() Function in Arduino

We can use the concat() function to concatenate two strings in Arduino. The concat() function will append the given parameter with a string.

It will return true if the concatenation operation has succeeded and false if it failed.

The basic syntax of the concat() function is shown below.

MyString.concat(MyParameter);

In the above syntax, the MyString variable is a string object in which a string is stored, and the MyParameter variable contains the parameter that we want to append with the string. The parameter can be the data type long, int, double, float, char, byte, and string.

Note that the string stored inside the MyString variable will be changed after the concat() function is executed because the given parameter will be attached to it, and the result will be saved inside the MyString variable.

Suppose we don’t want to change our original string. In that case, we can create another string variable that will also contain the same string as the first variable, and we will use the second string variable inside the concat() function.

For example, let’s create two string variables and concatenate them using the concat() function. See the code below.

void setup() {
  String s1 = "hello";
  String s2 = " World";
  s1.concat(s2);
  Serial.begin(9600);
  Serial.println(s1);
}
void loop() {

}

Output:

hello World

In the above code, we used the serial monitor of Arduino to show the result of concatenation. We have used space in the second string variable, which will also appear between the two strings in the output.

We can also use the output of the concat() function inside a conditional statement to check if the concatenation operation has succeeded or failed.

For example, we can use the if statement to check the output of the concat() function. If the output is true, we will print a message showing that the operation has been successful; if the output is false, we will print that the operation is not successful.

For example, let’s concatenate two strings and show a message of success or failure depending on the output of the concat() function. See the code below.

void setup() {
  String s1 = "hello";
  String s2 = " World";
  bool b = s1.concat(s2);
  Serial.begin(9600);
  Serial.println(s1);
  if(b){
    Serial.println("operation has been successful");
  }
  else {
    Serial.println("operation is not successful");
  }
}
void loop() {

}

Output:

hello World
operation has been successful

In the above code, we used a boolean b variable to store the output of the concat() function, and we used an if statement to check the boolean. If the output is true, the successful message will be printed on the serial monitor, and if the output is false, the other message will be printed on the serial monitor.

In the above output, we can see that the concatenation is successful. Check this link for more details about the concat() function.

Concatenate Strings Using the Append Operator + in Arduino

We can also use the append operator + to concatenate strings or variables of other data types and the allowed data types are the same as the concat() function. We can also concatenate multiple strings or variables of other data types in a single line using the append operator multiple times.

The basic syntax of the concatenation with the append operator is given below.

MyString = Parameter1 + parameter2 + ... + parameterN;

In the above syntax, the MyString variable is a string object used to store the output, and the parameters contain the value that we want to append with other parameters. The parameters can be the data types long, int, double, float, char, byte, and string.

For example, let’s create two string variables and one integer variable and concatenate them using the append operator. See the code below.

void setup() {
  String s1 = "hello";
  String s2 = " World";
  int n = 10;
  String s3 = s1 + " ,,," + s2 + " " + n;
  Serial.begin(9600);
  Serial.println(s3);
}
void loop() {
}

Output:

hello ,,, World 10

In the above code, we have created a string object to store the result of concatenation, and we have also used other strings like a string of three commas and a space. In the case of other data types, the append operator converts data types to the string data type and then appends them with the other string objects.

The drawback of appending multiple strings in one line is that it will also take a lot of memory, and the Arduino has significantly less memory. Check this link for more details about the append operator.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - Arduino String