Hour 8. Working with Strings


What You’ll Learn in This Hour:

Image What are strings?

Image Handling strings with C

Image The Arduino way to handle strings

Image Working with strings in your programs


One of the most confusing parts of the C programming language is working with text. Unfortunately, the C language uses a complex way of handling text, which can be somewhat confusing to beginner programmers. Fortunately, the Arduino developers realized that, and helped by creating some built-in string-handling features for the Arduino programming language. This hour first takes a look at how the C language handles strings, and then demonstrates how it’s much easier to work with strings in the Arduino environment.

What’s a String?

Whereas computers only understand and work with numbers, humans need words to communicate ideas. The trick to interfacing computer programs with humans is to force the computer to produce some type of output that humans can understand.

Any time you need your program to interface with humans, you need to use some type of text output. To do that with a computer program requires the use of the character data type.

The character data type stores letters, numbers, and symbols as a numeric value. The trick comes in using a standard way of mapping numeric values to language characters. One of the more popular character mapping standards for the English-speaking world is the ASCII format.

With ASCII, each character in the English alphabet, each numeric digit, and a handful of special characters, are each assigned a numeric value. The computer can interpret the ASCII number representation of the character to display the proper text based on the value stored in memory.

To store a character in memory, you use the char data type:

char letter = 'a';

When you use this statement, the Arduino doesn’t store the actual letter a in memory; it stores a numeric representation of the letter a. When you retrieve the letter variable and display it using the Serial.print function, the Arduino retrieves the numeric value, converts it to the letter a, and then displays it.

The next step in the process is storing words and sentences. A string value is a series of characters put together to create a word or sentence. The Arduino stores the multiple characters required to create the word or sentence consecutively in memory, then retrieves them in the same order they were stored.

The problem is that the Arduino needs to know just when a string of characters that make up the word or sentence stored in memory ends. Remember, the individual characters are stored as just numbers, so the Arduino processor has no idea of what number in memory represents a character and what doesn’t.

The solution is null-terminated strings. In the C programming language, when a string value is stored in memory, a 0 value is placed in the memory location at the end of the characters in the string value (this is called a null value). That way, the Arduino processor knows that when it gets to the null value, the string value is complete.

The next section shows just how to store and work with string values using the standard C programming language.

Understanding C-Style Strings

As you saw in the preceding section, the trick to creating strings in the C programming language is to store a series of characters in memory and then handle them as a single value. In Hour 7, “Programming Loops,” you were introduced to the idea of arrays. You can use character arrays to create and work with strings in the C programming language.

To refresh your memory, an array is a series of values of the same data type stored in memory, all associated with the same variable name. You can reference an individual value in the array by specifying its index value in square brackets in the variable name:

myarray[0] = 10;

The examples in Hour 7 only used numeric values in arrays, but you can use the same method with character values.

This section shows you how to use the C programming method to create and work with strings in your Arduino programs.

Creating Character Strings

A string is nothing more than an array of character values, with the last array data value being a 0 to indicate the null terminator. You can create a string character array using several different formats. To define a simple string, you just create an array with the individual letters of the string, with a null character as the last array value:

char test[7] = {'A', ' ', 't', 'e', 's', 't', ''),

Notice that each character (including the space) is defined as a separate data element in the array. Also, note that the array size that you define must be large enough to hold all the characters in the string plus the terminating null character (represented using the symbol). To retrieve the stored string value, you just reference it by its array variable name:

Serial.println(test);

Although this works, it is somewhat of a hassle to have to spell out each character in the string as a separate data element in the array. Fortunately, the C language provides a shortcut:

char test[15] = "This is a test";

The C compiler knows to create an array out of the characters listed within the double quotes and automatically adds the terminating null character at the end of the array. However, just as with the long format, you must declare the size of the array to be large enough to hold all the characters plus the null character.

Some C compilers (including the one used in the Arduino) allow you to initialize a string array without defining the size:

char test[] = "Testing the Arduino string";

The compiler automatically reserves enough space in memory to hold the declared string characters and the terminating null character.


By The Way: Strings, Characters, and Quotes

You’ll notice in the examples that I used single quotes around the individual characters in the array, but double quotes around the full string value. That’s a requirement for the C language, so be careful when defining character and string values!


When you initialize the string, you can make the array size larger than the initial string value:

char test[20] = "This is a test";

That allows you to change the string value stored in the variable later on in your sketch code to something larger.


Watch Out: Overflowing String Values

You must take great care when working with character arrays. The C compiler will allow you to store a value larger than the defined array size without generating an error message. However, the extra characters will “overflow” into the memory location for other stored variables, causing interesting issues with your sketch.


Finally, you can declare a character array without initializing it with a value:

char test3[20];

This reserves 20 bytes of memory for the string value storage. The downside to just declaring a character array is that you can’t just assign a character array value to it. Unfortunately, the C compiler can’t determine how to store the string value in the reserved character array. However, some common functions available in the C language provide ways for you to manipulate existing character arrays. The next section shows how to do that.

Working with Character Arrays

After you’ve created a character array in memory, you’ll most likely want to be able to use it in your Arduino programs. To reference the string as a whole, you can just use the character array variable name. Here’s an example of how to do that.

The output of the sketch should show the two strings, each string on a separate line, as shown in Figure 8.1.

Image

FIGURE 8.1 Output from running the sketch0801 code.

Once you declare the character array for the string and assign it a value, you cannot use standard assignment statements to change the character array like you would with numeric variables.

Instead, you have to use some of the built-in C functions designed for manipulating string values. Table 8.1 lists the string functions that the Arduino programming language supports.

Image

TABLE 8.1 The Arduino String Functions

Here’s an example of using the strcpy string function to change the value stored in a string variable in an Arduino sketch.

The output of the sketch should show the two strings, each string on a separate line, as shown in Figure 8.2.

Image

FIGURE 8.2 Output from running the sketch0802 code.

The sketch0802 sketch uses the random function to return a random number between 0 and 100. The if-then statement checks to see whether the number is less than 50, and uses the strcpy function to copy a message into the msg variable. This shows that you can dynamically change the value stored in a character array in your sketch, which can come in handy.


Watch Out: Copying Strings

Be careful when using the strcpy function to copy string values into character arrays. It’s important to remember about the character array size limitation. If you try to copy too large of a string value, you’ll overflow the array area in memory.


Comparing Character Arrays

One of the more popular functions you’ll need to do with character arrays is to compare them. You do that using the strcmp function. How you use it is a little odd, though, so let’s work through an example of using it in your Arduino sketches.

The format of the strcmp function is fairly simple:

strcmp(string1, string2)

What’s tricky about the strcmp function is the value that it returns:

Image A 0 if the two string values are equal

Image A negative number if string1 is less than string2

Image A positive number if string1 is greater than string2

A common mistake made by beginning C programmers is to write something like this:

if (strcmp(string1, string2))  // the wrong way to use the strcmp function

The problem with this statement is that if the two string values are equal, the strcmp function returns a 0 value, which the if-then statement interprets as a false value! If you want to compare two character arrays for equality, you want to use the following:

if (strcmp(string1, string2) == 0)

Now the Arduino will run the “then” code block if the two string values are equal.

Introducing the Arduino String Object

Working with the C language character arrays to handle text in your sketches is not for the faint of heart. Quite a lot of things can (and often do) go wrong if you’re not careful.

Fortunately, the Arduino developers took pity on the novice programmers that Arduino users often are and worked hard to create an easier way to create and work with strings in our Arduino sketches. Instead of using an array of character values to handle strings, the Arduino developers created a robust class object that does most of the hard work for us behind the scenes. This section shows you how to use the String object to make working with strings much easier.

The String Class

The Arduino String class allows us to easily create and work with just about any type of string value without the hassles involved with using C-style character arrays.

The easiest way to create a String object is to declare a variable using the String data type and assign it a value:

String test1 = "This is a test string";

The String object also has a constructor method named String that can convert just about any data type value into a String object:

String test2 = String('a'),
String test3 = String("This is a string of characters");
String test4 = String(31);
String test5 = String(20, HEX);

The result of all these declaration statements is a String object. The last example defines the number base of the value used as the first parameter.

One nice feature of the String class is that unlike character arrays, you can use it in normal assignment statements:

String string1 = "test";
string1 = "a new test";

The value stored in the string1 object changes in the assignment statement without you having to worry about string overflow; the Arduino takes care of that for you. Let’s take a look at an example of using the String object in an Arduino sketch.

The sketch creates three String objects and manipulates the values in them. Notice in line 7 that the code replaces the value originally stored in the string1 variable with a longer string value without causing any overflow problems. Lines 9 and 12 demonstrate how to concatenate multiple string values into a single value. When you open the serial monitor, you should see the output shown in Figure 8.3.

Image

FIGURE 8.3 The output from the sketch0803 code.

Once you declare a String object, you can use many different handy methods to manipulate those objects. The next section shows how to do that.

Various String Object Methods

After you create a String object, you can use several different methods to retrieve information about the stored String object. Table 8.2 shows these methods.

Image
Image

TABLE 8.2 The String Object Methods

The String object methods make working with strings a breeze. Remember the ugly strcmp function we had to use with the character array method? The equals function solves that silliness. Here’s an example of how to use that in your sketches:

String string1 = "This is a test";
String string2 = "this is a test";
if (string1.equals(string2))
   Serial.println("The strings are exactly equal");
if (string1.equalsIgnoreCase(string2))
   Serial.println("The strings are the same without regard to case");

If you run this code in your sketch, the first if-then condition check will fail, because the two string values aren’t exactly the same. However, using the equalsIgnoreCase function allows us to compare the string values without regard to case. That comes in handy if you need to check answers entered by users, such as yes/no responses to queries.

Manipulating String Objects

Besides the string methods that return answers based on the string value, a handful of methods actually alter the existing string value stored in the String object. Table 8.3 shows the different String class methods you can use to manipulate a String object.

Image

TABLE 8.3 The String Object Manipulation Methods

All of these functions enable you to manipulate the string value stored in a variable by changing the characters stored in the object.


Watch Out: Replacing String Values

Be careful with the string manipulation methods, because they replace the original string value with the result. The original string value will be lost in the process.


To get an idea of just how this works, let’s use the toUpperCase method in an example.

You should see the output shown in Figure 8.4 in your serial monitor window.

Image

FIGURE 8.4 Output from the sketch0804 code.

This example demonstrates how you can manipulate a String object value using the methods in the String object class.

Summary

This hour explored the world of using strings in your Arduino sketches. The C style of strings requires that you create a character array to store the individual characters in the string, terminated with a null character. This method can be cumbersome to work with, and can cause problems if you’re not careful. You then learned how to use the Arduino String object to create and work with string values. With String objects, you don’t have to worry about the string size, plus there are many more functions that allow you to easily compare and manipulate string values, which makes working with text a lot easier.

The next hour delves into the world of data structures. Sometimes you’ll want to be able to group data elements together as a single object, such as for storing data. Data structures allow you to do just that.

Workshop

Quiz

1. Which String class method should you use to emulate the strcmp character array function?

A. compareTo

B. equals

C. equalsIgnoreCase

D. indexOf

2. The Arduino compiler won’t allow you to store more characters in a character array than the size of the array you defined. True or false?

3. How can you check whether a user answers YES or yes to a question?

Answers

1. The compareTo method returns a 0 if the two string values match, a negative value if the string is smaller than the compared value, and a positive value if the string is larger than the compared value. This behavior emulates the strcmp function used for character arrays.

2. False. The compiler will allow you to store more characters in the array memory location than the size you defined for the character array. The extra characters will “overflow” into the memory location for other variables, causing problems in your programs, so be careful.

3. Use the equalsIgnoreCase method to compare the answer string to a yes string value.

Q&A

Q. Does it matter whether I use a character array or a String object in my sketches?

A. For functional purposes, no; both methods will work the same. However, String objects do take up more memory space than character arrays. So if you’re writing a large sketch and are tight on memory, you might want to use character arrays.

Q. Can I create an array of character arrays and then access each string value separately?

A. Yes, but that requires a feature called pointers, which is discussed in Hour 11, “Pointing to Data.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.100