Arduino – strings

Strings, (string, lowercase) are practically arrays of characters, as discussed in the previous section, we use strings to store text. These texts can then be used in our arduino program for other interactions, such as storing characters read from a keyboard or writing to a serial monitor, LCD.

It can be declared according to the following examples. The last element of the character arrays is ‘\ 0’ (zero), from where the compiler recognizes the end of the string. If we do not enter zero, the compiler will do it for us. Characters must be written between apostrophes in arrays.

char myString[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '
char myString[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
char myString[12] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
'}; char myString[12] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};

The string array is e.g. can be printed to the serial monitor using Serial.println().

Serial.println(myString);

We can also describe the above string in the array more simply and transparently. Strings must be enclosed in quotation marks.

char myString[] = "Hello World";
char myString[] = 
   "First line "
   "Second line "
   "Third line ";

You can change the contents of a string array at runtime, as in the example below. First, we enter the trailing zero in index 5 of the original string array, thereby deleting the second part of the string, the space and “World” string. then we add new characters from index 5.

char myString[] = "Hello World";

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

  myString[5] = 0;
  Serial.println(myString);

  myString[5] = ' ';
  myString[6] = 'A';
  myString[7] = 'r';
  myString[8] = 'd';
  myString[9] = 'u';
  myString[10] = 'i';
  myString[11] = 'n';
  myString[12] = 'o';
  myString[13] = 0;
  Serial.println(myString);  // print to console: Hello Arduino
}
void loop() 
{

}

You can also use a couple of built-in functions for strings. We can manipulate the string array with these “C” functions, but we can also learn information about them.

Use the sizeof () operator to query the length of the array containing the string. The length includes zero character to indicate the end of the array, so its length is one more than the length of the string.

Serial.println(sizeof(myString));

Use the strlen () function to specify the length of the string. The string length applies only to printable characters and does not include the zero character at the end of the array.

Serial.print(strlen(myString));

The strcpy () function has two parameters. Copies the contents of the second parameter to the first parameter. This allows you to copy the string to a larger array and append another string later.

strcpy(myNewString, myString);
Serial.println(myNewString);

You can use the strcat () function to concatenate two strings. The strcat () function appends the string in the second parameter to the end of the string in the first parameter. Make sure that the size of the array passed in the first parameter is sufficient to store both strings.

strcat(myNewString, " appends this string");

In the next section, we will look at using the much more efficient Arduino String () object.