Javatpoint Logo
Javatpoint Logo

Arduino String

The string is a data type that stores text rather than the integer values. It is similar to other data types such as integer, float, etc., in Arduino.

The string is an array of characters or a set of characters that consists of numbers, spaces, and special characters from the ASCII table.

The string can be declared in the following ways:

  • char StrA[10];
  • char StringA[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};

We can also add an explicit null character

  • char StringB[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e', '\0'};

We can also declare an array with extra space for a string constant StrA.

  • char StrA[10]; = "Hello";
  • char StrD[6]; = "Hello";
  • char StrD[]; = "Welcome";

The data type is char. Each character is an element in string. For example, Arduino contains seven characters. So, we need to declare an array atleast of size 8 (1 extra to store the null character at the end of the string).

Consider the below example:

The above declared string will be stored as:

Arduino String

When we create a string using the double quotes like the word "text" specified above, the compiler automatically creates an element out of each character. It further appends the null character to the end.

The null character has the value 0 in the ASCII table. It is defined using two characters ( \ and \0). The backslash (\) represents the special character when used in conjunction with other characters. The backslash is also known as an escape character. The null character is added by the compiler to terminate the string.

Note: Strings are always declared inside the double quotes, while characters are declared inside single quotes.

How Serial.print() and Serial.println() works with strings?

It is interesting to know how print() and println() function works with the strings.

Let's start.

Serial.print() with string

Let's understand with a coding example.

The code will print Hello Arduino multiple times, as shown below:

Arduino String

Serial.println() with string

In the case of println() function, we need not require any for loop or condition to print.

It is an easier way to print strings.

The println() function will work the same as the above.

Consider the below example.

Output:

The output will be the same as the above.

Array of Strings

We can specify the array of strings while working with large projects, such as LCD display, etc.

The array names are also considered as pointers. The data type marked with an asterisk is defined as pointers.

For example, char*. To define an array of arrays, we actually need pointers.

Let's understand with an example.

Consider the below code:

Output:

Arduino String




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA