Arduino String Functionality

To display text on an LCD or Serial Monitor of Arduino IDE by a sketch or program we use string function. To store user inputs like name and other details we also use string.

The string is a text. To store the text we use Strings. Have you seen a calculator or digital gadget which has any text displayed on its LCD?

There are two types of string used in Arduino programming:-

  • An array which is the same as C programming.
  • Arduino String.

In this tutorial, we will learn about Strings and how to use a string in the Arduino sketch.

Here some more tutorials for you:


String Character Arrays

Arduino has an added capability for using an array of characters known as String that can store and manipulate text strings. The String is an array of char variables. The char is a data type that stores an array of string.

The array of string has one extra element at the end and represented by value 0 (zero). The last element 0 (zero) known as a null terminator.

The following sketch shows an array of char type of 5 elements and 6th elements is 0 (zero) as a null terminator where the string ends. The serial.println function prints the string array on the serial monitor of the IDE.

The above sketch is also the same and more convenient than the first one. The compiler calculates the size of the array and automatically terminates the string with zero. It also gives the same output.

Output

Hello

Manipulating String Array

Altering the String text is called the manipulation of the string array. Manipulation means to add or delete an element of a string array. Let’s see below example sketch:

Output

I love arduino and programming
I love arduino
I love arduino coding

The above sketch work in the following ways:

  1. Creating and Printing String: At first, we create a new string and print it at Serial Monitor using Serial.println() function.
  2. Shortening or Deleting: We delete a part of the string. We shortening the string by replacing 15th elements (space) with null termination 0 (zero). This is the 14th element in the array when we count from 0.
  3. Changing a Word in String: Now in this step we replace the “programming” word with “coding” word in the string array. First, we replace the null terminator my_str[14] with space.

Functions to Manipulate String Arrays

In the previous sketch, we manipulate the string in a manual way by accessing the individual character of the string. Now we use own functions and c library to manipulate the string. We also calculate the length of the string.

Output

This is my string
String length is: 17
Size of the array: 18
This is my string
This is my string sketch.
String length is: 25
Size of the array out_str[]: 40

The above sketch works in the following ways:

  1. Print the String: The created string is printed in the Serial Monitor of IDE.
  2. Get the Length of the String: By using strlen() function we get the string length. It does not include the null terminator.
  3. Get the Length of the Array: We use sizeof() function to get the length of the array. It also includes the null terminator.
  4. Copy a String: The strcpy() function is used to copy the str[] string into out_str[] array. The out_str[] have total 40 char elements.
  5. Append a string to a String (Concatenate): Concatenation means joining one string to another. We use strcat() function to concatenate strings.

Arduino String Object

The second type of string used in Arduino programming is the String object.
An object is a construct that contains both data and function. A string object is used to initialize a text value.

The following example is will make clear all your doubts.

Output

This is my string
THIS IS MY STRING
My new string.
My new Arduino sketch.
String length is: 22
My new Arduino sketch. and this is second string

The above sketch works in the following way:

The String object creates a string.

  1. Print the String: To print the string on the Serial Monitor.
  2. Change to Upper Case: This function changes the string characters to upper case.
  3. Overwrite the String: These functions overwrite the string by another string.
  4. Replace String Word: To replace the string word by another one we use this function.
  5. Length of String: The my_str.length() function returns the string length.
  6. Join two Strings (Concatenate): It appends the contents of another string to the end of the first string, concat is short for concatenate.
Scroll to Top