Strings

Strings are used to store text. They can be used to display text on an LCD or in the Arduino IDE Serial Monitor window. Strings are also useful for storing the user input. There are two types of strings in Arduino Programming:

  • Arrays of characters, which are the same as the strings used in C programming.
  • The Arduino String, which lets us use a string object in a sketch.
String Character Array

A string is an array of char variables.

A string is a special array that has one extra element at the end of the string, which always has the value of 0 (zero). This is known as a “null terminated string”.

void setup()

{

char my_str[] = "Ask4Electrix";
Serial.begin(9600);

Serial.println(my_str);
}
void loop()
{
}

With this for format, the compiler calculates the size of the string array and also automatically null terminates the string with a zero.

Functions to manipulate String Arrays

Following functions are used as my_str.*, where * may be one of the following functions.

String()

The String class allows you to use and manipulate strings of text in more complex ways than character arrays do. You can concatenate Strings, append to them, search for and replace substrings, and more. It takes more memory than a simple character array, but it is also more useful.

For reference, character arrays are referred to as strings with a small ‘s’, and instances of the String class are referred to as Strings with a capital S. Note that constant strings, specified in “double quotes” are treated as char arrays, not instances of the String class.

charAt()

Access a particular character of the String.

compareTo()

Compares two Strings, testing whether one comes before or after the other, or whether they are equal. The strings are compared character by character, using the ASCII values of the characters. That means, for example, ‘a’ comes before ‘b’ but after ‘A’. Numbers come before letters.

concat()

Appends the parameter to a String.

c_str()

Converts the contents of a string as a C-style, null-terminated string. Note that this gives direct access to the internal String buffer and should be used with care. In particular, you should never modify the string through the pointer returned. When you modify the String object, or when it is destroyed, any pointer previously returned by c_str() becomes invalid and should not be used any longer.

endsWith()

Tests whether or not a String ends with the characters of another String.

equals()

Compares two strings for equality. The comparison is case- sensitive, meaning the String “hello” is not equal to the String “HELLO”.

equalsIgnoreCase()

Compares two strings for equality. The comparison is not case- sensitive, meaning the String(“hello”) is equal to the String(“HELLO”).

getBytes()

Copies the string’s characters to the supplied buffer.

indexOf()

Locates a character or String within another String. By default, it searches from the beginning of the String, but can also start from a given index, allowing to locate all instances of the character or String.

lastIndexOf()

Locates a character or String within another String. By default, it searches from the end of the String, but can also work backwards from a given index, allowing to locate all instances of the character or String.

length()

Returns the length of the String, in characters. (Note that this does not include a trailing null character.)

remove()

Modify in place, a string removing chars from the provided index to the end of the string or from the provided index to index plus count.

replace()

The String replace() function allows you to replace all instances of a given character with another character. You can also use replace to replace substrings of a string with a different substring.

reserve()

The String reserve() function allows you to allocate a buffer in memory for manipulating strings.

setCharAt()

Sets a character of the String. Has no effect on indices outside the existing length of the string.

startsWith()

Tests whether or not a String starts with the characters of another string.

toCharArray()

Copies the string’s characters to the supplied buffer.

substring()

Get a substring of a String. The starting index is inclusive (the corresponding character is included in the substring), but the optional ending index is exclusive (the corresponding character is not included in the substring). If the ending index is omitted, the substring continues to the end of the String.

toInt()

Converts a valid String to an integer. The input string should start with an integer number. If the string contains non-integer numbers, the function will stop performing the conversion.

toFloat()

Converts a valid String to a float. The input string should start with a digit. If the string contains non-digit characters, the function will stop performing the conversion. For example, the strings “123.45”, “123”, and “123fish” are converted to 123.45, 123.00, and 123.00 respectively. Note that “123.456” is approximated with 123.46. Note too that floats have only 6-7 decimal digits of precision and that longer strings might be truncated.

toLowerCase()

Get a lower-case version of a String. As of 1.0, toLowerCase() modifies the string in place rather than returning a new.

toUpperCase()

Get a upper-case version of a String. As of 1.0, toUpperCase() modifies the string in place rather than returning a new.

trim()

Get a version of the String with any leading and trailing whitespace removed. As of 1.0, trim() modifies the string in place rather than returning a new one.

A String object is much easier to use than a string character array. The object has built-in functions that can perform a number of operations on strings.

quickly use up the Arduino RAM memory, which may cause Arduino to hang, crash or behave unexpectedly. If a sketch on an Arduino is small and limits the use of objects, then there should be no problems.

 

Advertisements