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.
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:
I love arduino and programming I love arduino I love arduino coding
The above sketch work in the following ways:
- Creating and Printing String: At first, we create a new string and print it at Serial Monitor using Serial.println() function.
- 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.
- 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 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.
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:
- Print the String: The created string is printed in the Serial Monitor of IDE.
- Get the Length of the String: By using strlen() function we get the string length. It does not include the null terminator.
- Get the Length of the Array: We use sizeof() function to get the length of the array. It also includes the null terminator.
- 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.
- 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.
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.
- Print the String: To print the string on the Serial Monitor.
- Change to Upper Case: This function changes the string characters to upper case.
- Overwrite the String: These functions overwrite the string by another string.
- Replace String Word: To replace the string word by another one we use this function.
- Length of String: The my_str.length() function returns the string length.
- Join two Strings (Concatenate): It appends the contents of another string to the end of the first string, concat is short for concatenate.