Arduino

What are the Strings and String Functions in Arduino? Give Examples.

Strings are the data types that are used to store the characters and a series of characters in them. The working of Strings in Arduino is similar to “char” in C programming. The values stored in the Arduino can be displayed on the LCD as well as on serial monitor output.

The Strings used in Arduino are of two types which are discussed in this write-up. We will discuss the functions of Strings with the help of examples.

What are the types of strings in Arduino

In Arduino, strings are divided into two types which are Object Strings or character strings. The difference in both of them is, the “Object Strings” is used to store the series of characters where the character strings also known as c-strings store the string in the form of characters.

To understand it more clearly, consider the following Arduino code:

void setup(){

Serial.begin(9600);

String Title="This is LinuxHint";

Serial.println(Title);

}

void loop(){

}

The output of the above Arduino code:

In the above code, we have declared an Object String; “Title” and store “This is LinuxHint”. With the help of Serial.begin(9600), we initialize the serial communication to Arduino through the USB port at boude rate of 9600. And using the serial communication printed the stored values in “Title” on the serial monitor output.

Similarly, to store the value using the character string:

void setup(){

Serial.begin(9600);

char Title[]="This is LinuxHint";

Serial.println(Title);

}

void loop(){

}

The explanation of the above output is as; we used the char data type and with the help of an array stored “This is LinuxHint” in the Title variable.

Functions of String in Arduino

There are different built-in functions of Strings in Arduino and these functions take input of two strings and after performing some specific function, return the single output. Some of the important functions are:

concat(): This function is used to joined two strings together to form a single string, for example, we have two strings; string_A and string_B. We will combine both the strings and the result of this function will be displayed on serial monitor output using the code:

void setup(){Serial.begin(9600);

String string_A, string_B, string_C;

string_A="This is LinuxHint";

string_B="You are reading Arduino tutorials";

string_A.concat("");

string_A.concat(string_B);

Serial.println(string_A);

}

void loop(){

}


In the above code, we first declared three variables of the string data type. Store some values in string_A and string_B and then we concatenate the string_A and string_B using the concat() function. The concat() function joins the string to another string as in the above code, we added some space in string_A and then added string_B value in string_A. Finally, using serial communication, I printed the results.

length(): This function is used to calculate the length of the specific string, it counts the number of characters used in the string and returns the integer value. Consider an example:

String arr="Welcome to LinuxHint!";void setup() {

Serial.begin(9600);

Serial.print("String length:");

int arr_len= arr.length();

Serial.println(arr_len);

}

void loop(){

}


The above code displays the length of the string is 21.

toUpperCase() and toLowerCase(): We can change the case sensitivity of the string using these functions. We will consider an example, in which we will change the case sensitivity of string to upper case using the toUpperCase() function:

void setup(){Serial.begin(9600);

String string_A;

string_A="This is LinuxHint";

string_A.toUpperCase();

Serial.println(string_A);

}

void loop(){

}

Similarly, we can change the value of the string to lowercase using the “toLowerCase()” function.

compareTo():  This function is used to find out the length of the string and it compares the strings on the basis of their character. The ASCII values of the characters are considered which means we know “a” comes before “b”, but it comes after “A”because the ASCII code of “a” is 97 and “A” is 65. Consider an example:

void setup(){Serial.begin(9600);

String string_A, string_B, string_C;

string_A="This is LinuxHint";

string_B="You are reading Arduino tutorials";

string_C="This is LinuxHint";

Serial.println("Comparison result of string_A with string_B: ");

Serial.println(string_A.compareTo(string_B));

Serial.println("Comparison result of string_A with string_C: ");

Serial.println(string_A.compareTo(string_C));

}

void loop(){

}

From the result displayed in the output, it comes to know that string_A and string_c are equal so it returns the “0” and in comparison of string_A and string_B, “-5” means the value of string_A comes before the value of string_B.

There are some other functions that are used with the strings:

Functions Explanation
charAt(n) This function is used to access any particular character of the string
endsWith() If both the strings end with the same characters, it returns true
equals() If both the strings are equal, it returns true. It should be noted that this function is case sensitive which means “hammad” and “HAMMAD” are not equal
equalsignorecase() It works similar to the equals() function but it is not case sensitive which means “hammad” and “HAMMAD” both are equal
replace() It replaces all the instances of string A with string B
startsWith() It returns true if both the strings start with the same character
remove() It is used to remove any character of the string from any particular index
reserve() It reserves a buffer for the manipulating of the string in memory
toCharArray It copies the entire characters of the string to the buffer
toInt It is used to convert a string to an integer
trim() It is used to trim any part of the string

Conclusion

Strings are used in Arduino for storing the series of characters which can be used for many purposes such as to display the output on LCD interface with Arduino. In this write-up, we have explained both types of strings with examples. Also, discuss in detail some important functions used in Object Strings.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.