In programming, a string is simply a series of characters grouped together. Strings provide a convenient way for storing groups of characters, like words or sentences. When dealing with strings, there are two primary types to worry about. The first type is the “C-string.” This kind of string consists of an array of char types. If you are unfamiliar with arrays, follow the red tab on the right. The second type of string is the String class. This type of string is an instance of the String class rather than an array of chars. The primary difference between these C-strings and String objects are their efficiency and ease of use. Typically, String objects are easier to use since they have a lot supporting functions (called methods) built into them. The tradeoff is that String objects typically use more memory compared to C-strings. If conserving memory is an issue, C-strings are more efficient to use, but they are less convenient. They do not come with any built in methods so you either have to write them yourself, or import a supporting library. The specific details of each type of string are explained further in the following sections.
As stated earlier, a C-string is simply a group of char types stored in an array. Although C-strings are made from char arrays, not every char array can be considered a string. To differentiate a C-string from a char array, the last character in a C-string is always the null character '\0' (backslash zero). This is why C-strings are sometimes called “Null-terminated” strings. The null termination provides an easy way to tell where the string's end is. Typically, functions that deal with C-strings always look for this null terminator. An example is the Serial.print() function. When the Serial.print() is provided with a C-string, it grabs each character in the string one at a time. It knows to stop once it reads the null character '\0'. If the null character is not present, then Serial.print() will exceed the boundaries of the char array and print whatever nonsense it finds in memory. To prevent malfunctions like the aforementioned, be sure instantiate a C-string and not a char array. There are a four ways to ensure you are defining a C-string, which are shown below.
//C-string definitions
//Method 1: explicitly add the null character to the end
char Str1[4] = {'a', 'b', 'c', '\o'};
//Method 2: let the compiler add the null character
char Str2[4] = {'a', 'b', 'c'};
//Method 3: use "" notation
char Str3[ ] = "abc";
char Str4[4] = "abc";
//This definition will produce an error
char Str5[3] = "abc";// <-- error: initializer-string for array of chars is too long
//Method 4:define an empty string that can be 6 characters long
char Str6[7];
//Method 5: Wrap strings
char myString[] = "abcdefg"
"hijklmnop"
"qrstuvwxyz";
On a side note, be aware of which quotations you use when defining strings. The double quotations are always used to indicate a string while single quotations are always used to indicate an individual character.
Most of the time C-strings are more trouble than they are worth. It is much easier to use class based Strings. To help illustrate the time String objects can save, we will examine a few functions that are commonly written to manipulate C-strings.
Sometimes it is useful to know the length of a string that you are working with. To determine a C-string's length, you must use a function like the one shown below.
int stringLen(char string[ ])
{
int i = 0;
while (string[i] != '/0')
{
i++;
}
return i;
}
Copying one string to another is essentially the same as copying arrays. To do so, you must copy one character at a time using a loop and both strings must be the same length.
//Copy Str1 to Str2
char Str1[ ] = "abc";
char Str2[ ] = "123";
if(stringLen(Str1) == stringLen(Str2))// check if strings are same size
{
int i = 0;
while (Str1[i] != '/0')
{
Str2[i] = Str1[i];
i++;
}
}
To compare two strings, they must be the same length and must be compared one character at a time.
boolean stringCompare(char Str1[ ], char Str2[ ])
{
if(stringLen(Str1) == stringLen(Str2))// check string size
{
int i = 0;
while (Str1[i] != '/0')
{
if (Str1[i] != Str2[i]) //if characters mismatch the strings are different
{
return false;
}
i++;
}
return true;// if you get here then all the characters match
}
else //strings are unequal in size and therefore not the same
{
return false;
}
}
The class based String works similarly to C-strings. String objects provide all the functionality of a C-string plus more. The catch is that they use more memory, but this typically isn't a major concern. To instantiate a String object, simply do the following.
//Define initialized String object
String Str1 = "abcd";
//Define empty String object
String Str2 = "";
After defining a String object, you can use special operators to manipulate the String. For example, you can use square brackets (“[]”) to access individual characters, just like with C-strings and arrays. Unlike C-Strings, String objects also have a few extra operators which include the comparison operator “==”, the assignment operator “=”, and the concatenation operator &ldquo+”. The comparison operator allows you to directly compare two String objects without calling a function. For example:
String Str1 = "abc";
String Str2 = "123";
boolean stringEqual;
//compare Str1 and Str2
if(Str1 == Str2)
{
stringEqual = true;
}
else
{
stringEqual = false;
}
The String class assignment operator allows you to assign or copy an existing String without the use of a loop. For example:
String Str1 = "abc";
String Str2 = "123";
//Copy Str1 to Str2
Str2 = Str1;
Finally, the concatenation operator lets you combine two separate String objects into one. You can also use it to append individual characters. For example:
String Str1 = "abc";
String Str2 = "123";
String Str3 = "";
String Str4 = "";
String Str5 = "";
//Concatenate Str1 to Str2
Str3 = Str1 + Str2;//Result Stored in Str3: "abc123"
//Concatenate Str2 to Str1
Str4 = Str2 + Str1;//Result Stored in Str4: "abc123"
//Concatenate Str2, Str2 and a constant string
Str5 = Str2 + " -:- " + Str1;//Result Stored in Str5: "abc -:- 123"
//Append a character onto the end of Str3
Str3 = Str3 + '#';//Result Stored in Str3: "abc123#"
Multiple operators are not the only advantage that class based Strings possess. They also have built in methods that can come in very handy. For example, all String objects have a built in length() method. Just like with any other object, you need to use the dot operator to call this method. For example:
String Str1 = "abc";
int stringLen;
//calculate length of Str1 and store it in stringLen
stringLen = Str1.length();
The length() method is not the only one available to use. There are many more with various applications. For a complete list and description of each, follow the yellow tab on the right.