Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino string object


May 15, 2021 Arduino


Table of contents


The second type of string used in Arduino programming is the string object.

What is an object?

An object is a construct that contains data and functions. /b10> String objects can be created and assigned a value or string like variables. S tring Objects contain functions (called "methods" in object-oriented programming (OOP), which manipulate the string data contained in string objects.

The following sketches and explanations will clearly explain what an object is and how to use it with strings.

Example

void setup() { 
   String my_str = "This is my string.";
   Serial.begin(9600);

   // (1) print the string
   Serial.println(my_str);

   // (2) change the string to upper-case
   my_str.toUpperCase();
   Serial.println(my_str);

   // (3) overwrite the string
   my_str = "My new string.";
   Serial.println(my_str);

   // (4) replace a word in the string
   my_str.replace("string", "Arduino sketch");
   Serial.println(my_str);

   // (5) get the length of the string
   Serial.print("String length is: ");
   Serial.println(my_str.length());
}

void loop() { 

}

Results

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

Create a string object and assign a value (or string) at the top of the sketch.

String my_str = "This is my string." ;

This creates a My_str object called this is created and assigned "This is my string."

This can be compared to creating a variable and assigning it a value, such as an integer:

int my_var = 102;

The sketch above works in the following ways.

(1) Print the string

Strings can be printed to the serial monitor window like an array string of characters.

(2) Convert the string to capital

A string object is created my_str, and there are multiple functions or methods on which you can operate. /b10> These methods are called by using the object name followed by the point operator (.) and then by using the name of the function.

my_str.toUpperCase();

The toUpperCase() function manipulates strings contained in my_str objects of type String and converts the string data (or text) contained in the object into capital characters. /b11> The String class contains a list of functions that can be found in the Arduino string reference. /b12> Technically, String is called a class that is used to create String objects.

(3) Overwriting the string

The assignment operator is used to assign a new string to my_str object to replace the old string.

my_str = "My new string." ;

The assignment operator cannot be used for character array strings, only for String objects.

(4) Replace the words in the string

The replace() function is used to replace the second string passed to it with the first string passed to it. /b10> replace() is another function built in the String class, so it can be used on the my_str String object.

(5) Gets the length of the string

The length of the string can be easily obtained by using legth(). /b10> In the sample sketch, the results returned by legth() are passed directly to Serial.println(), without using intermediate variables.

When to use string objects

String objects are easier to use than an array of string characters. /b10> The object has built-in functions that can perform multiple operations on strings.

The main disadvantage of using the String object is that it uses a lot of memory and can quickly run out of Arduino RAM memory, which can cause Arduino to hang, crash, or behave unexpectedly. /b10> If the sketch on Arduino is small and restricts the use of the object, then there should be no problem.

Character array strings are more difficult to use, and you may need to write your own functions to manipulate these types of strings. /b10> The advantage is that you can control the size of the string array, so you can keep the array small to save memory.

You need to make sure that you don't go beyond the string array boundary, and the String object doesn't have this problem, so long as there's enough memory for it to operate, it takes care of your string boundary. W hen out of memory, a String object can try to write in memory that does not exist, but never beyond the end of the string in which it operates.

Where to use strings

In this chapter, we learned about strings, their behavior in memory, and their actions.

The use of strings will be covered in the next part of the course, where we'll learn how to get user input from the serial monitor window and save it as a string.