JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Concatenating strings in Arduino

Author:JIYIK Last Updated:2025/04/16 Views:

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


concat()Concatenate strings using the Arduino function

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

It returns if the connect operation succeeded, trueor 0 if it failed false.

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

MyString.concat(MyParameter);

In the above syntax, MyStringvariable is a string object, in which a string is stored, and MyParametervariable contains the parameter we want to append to the string. The parameter can be of data type long, int, double, float, char, byte, and string.

Note that concat()after the execution of the function, the string stored in MyStringthe variable will be changed because the given arguments will be appended to it and the result will be saved in MyStringthe variable.

Let's say we don't want to change the original string. In this case, we can create another string variable that also contains the same string as the first variable, and we will concat()use this second string variable in the function.

For example, let us create two string variables and concat()concatenate them using the 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 have used the Arduino serial monitor to display the result of the connection. We have used a 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 function in a conditional statement concat()to check whether the join operation succeeded or failed.

For example, we can use ifthe statement to check concat()the output of the function. If the output is true, we will print a message indicating that the operation was successful; if the output is false, we will print that the operation was unsuccessful.

For example, let us concatenate two strings and concat()display a success or failure message based on the output of the 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 use the Boolean variable bto store concat()the output of the function, and we use ifthe statement to check the Boolean value. If the output is true, a successful message will be printed on the serial monitor, and if the output is false, another message will be printed on the serial monitor.

In the above output, we can see that the connection was successful. Check out this link for concat()more details on the 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. The allowed data types concat()are the same as the function. We can also use the append operator to concatenate multiple strings or variables of other data types multiple times in a row.

Given below is the basic syntax for concatenation with the append operator.

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

In the above syntax, MyStringvariable is a string object to store the output and parameter contains the value to which we want to append other parameters. Parameters can be of data types long, int, double, float, char, byte, and string.

For example, let us create two string variables and an integer variable and concatenate them using the append operator. See the following code.

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 created a string object to store the concatenation result, and we also used other strings like a string of three commas and a space. In case of other data types, the append operator converts the data type to a string data type and then appends them with other string objects.

The downside of appending multiple strings in a row is that it will also use up a lot of memory, and Arduino has much less memory. Check out this link for more details on the append operator.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino Square Wave Generator

Publish Date:2025/04/16 Views:181 Category:C++

digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values ​​are instan

Splitting a string in Arduino

Publish Date:2025/04/16 Views:188 Category:C++

substring() This tutorial will discuss splitting a string using the function in Arduino . substring() Splitting a string in Arduino using the function Arduino provides an inbuilt function substring() to split a given string. We can split th

Comparing Strings in Arduino

Publish Date:2025/04/16 Views:137 Category:C++

compareTo() This tutorial will discuss comparing two strings using the function in Arduino . compareTo() Comparing strings using the Arduino function To compare two strings in Arduino, we can use compareTo() the function of the string objec

Arduino mills() function

Publish Date:2025/04/16 Views:156 Category:C++

This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial