fbpixel
Tags: ,

When you start programming with Arduino, you will very quickly get to the use of strings. A string is used to store text. It is used, for example, to display text on an LCD screen or to send text to the serial monitor. This is often the first example: the display of “Hello World!”, which is a string, in the serial monitor. In this tutorial we will see how to define and use this type of variable.

There are different ways to define a string in Arduino

  • array of characters of type char
  • String type defined in Arduino’s language

The String type is, in fact, an array of characters ending with a null character.

Char array

Throughout this tutorial, we will use the world’s most used example “Hello World” and dissect it at length.

Size of a char array

In order to study the channels we are going to use two very practical functions:

  • strlen to know the length of a string of characters
  • sizeof to know the size of the array containing the string
char str[]="Hello World!";

void setup() {
  Serial.begin(9600);
  Serial.println(str);
  Serial.print("String length :");Serial.println(strlen(str));
  Serial.print("Array length :");Serial.println(sizeof(str));
 for(int i=0;i<sizeof(str);i++){
    Serial.println(str[i]);
 }
 Serial.println(F(" end of string"));
}

void loop() {}

Thanks to this simple example, we are already noting some interesting information. The Arduino programme adds a null character at the end of the string.

Hello World!
String length :12
Array length :13
H
e
l
l
o
 
W
o
r
l
d
!

end of string

In this example, our string has a length of 12. If we explicitly define the length of the array, we need to add the null character at the end of the string.

char str[13]="Hello World!";

void setup() {
  Serial.begin(9600);
  Serial.println(str);
  Serial.print("String length :");Serial.println(strlen(str));
  Serial.print("Array length :");Serial.println(sizeof(str));
 for(int i=0;i<sizeof(str);i++){
    Serial.println(str[i]);
 }
 Serial.println(F("> end of string"));
}

void loop() {}

Hello World!
String length :12
Array length :13
H
e
l
l
o
 
W
o
r
l
d
!
> end of string

Changing an array of characters

The text in a character table cannot be changed at once, the new text must be assigned character by character.

char chars[]="Hello World!";

void setup() {
  Serial.begin(9600);
  Serial.println(F("Delete end of string"));
 chars[6]=0;
 Serial.println(chars);
 Serial.println(F("Replace word"));
 chars[6]='Y';
 chars[7]='o';
 chars[8]='u';
 chars[9]=0;
 Serial.println(chars);
  Serial.print("String length :");Serial.println(strlen(chars));
  Serial.print("Array length :");Serial.println(sizeof(chars));
 for(int i=0;i<sizeof(chars);i++){
    Serial.println(chars[i]);
 }
 Serial.println(F(" end of chars"));


}

void loop() {}

The size of the board keeps the initial size while the size of the chain is modified. If you scroll to the end of the array, you can see that the characters have not been deleted or replaced. Knowing this, be careful when handling the strings.

Delete end of string
Hello 
Replace word
Hello You
String length :9
Array length :13
H
e
l
l
o
 
Y
o
u

d
!

end of chars

Concatenation

Another way to replace text is to use the strcat() function, which adds one string to the end of another.

char chars[]="Hello World!";

void setup() {
  Serial.begin(9600);
  Serial.println(F("Delete end of string"));
 chars[6]=0;
 Serial.println(chars);
 Serial.println(F("Concat word"));
 strcat(chars,"You");
 Serial.println(chars);
  Serial.print("String length :");Serial.println(strlen(chars));
  Serial.print("Array length :");Serial.println(sizeof(chars));
 for(int i=0;i<sizeof(chars);i++){
    Serial.println(chars[i]);
 }
 Serial.println(F(" end of chars"));


}

void loop() {}

This example is equivalent to the previous code. The strcat function will change the char array. If you want to keep the original string you can define another string and copy the text to it.

Copy a text

char chars[]="Hello World!";
char str[20];
void setup() {
  Serial.begin(9600);
  strcpy(str,chars);
 str[6]=0;
 strcat(str,"You");
 Serial.println(str);
  Serial.print("String length :");Serial.println(strlen(str));
  Serial.print("Array length :");Serial.println(sizeof(str));
 Serial.println(chars);

}

void loop() {}

Hello You
String length :9
Array length :20
Hello World!

When the array of char is not defined, it will be necessary to define an array size so that the microprocessor can reserve the necessary memory.

Comparison of two char arrays

Even if the char arrays have different sizes, it is possible to compare them using the strcmp() function.

char chars[]="Hello World!";
char str[20];
void setup() {
  Serial.begin(9600);
  strcpy(str,chars);
 Serial.println(chars);

 if(strcmp(chars,str)==0){
   Serial.println("str and chars are the same");
 }

}

void loop() {}

N.B. the equivalent logic test “==” only works for single characters (e.g. char c=’c’; Serial.println(c==’c’);)

Functions to remember

  • sizeof(str) to know the size of the array
  • strlen(str) to know the length of a string in an array
  • strcat(str,str1) add str1 to the end of str
  • strcopy(str1,str) copy str into str1
  • strcmp(str,str1) to compare two arrays of characters

String Arduino

The String object is defined in the Arduino language and contains a set of practical functions for manipulating strings.

String size

In order to study the channels we are going to use two very practical functions:

  • str.length() to know the length of a string
  • sizeof to know the size of the variable
String str="Hello World!";
void setup() {
  Serial.begin(9600);
  Serial.println(str);
  Serial.print("String length :");Serial.println(str.length());
  Serial.print("Array length :");Serial.println(sizeof(str));
 for(int i=0;i<str.length();i++){
    Serial.println(str[i]);
 }
 Serial.println(F(" end of string"));
}

void loop() {}

Hello World!
String length :12
Array length :6
H
e
l
l
o
 
W
o
r
l
d
!
 end of string

Modification of a String

Editing a word with the String object is much easier than with the float board.

String str="Hello World!";

void setup() {
 Serial.begin(9600);
 Serial.println(str); 
 Serial.println(F("Replace word"));
 str.replace("World!","You");
 Serial.println(str); 
}

void loop() {}

Hello World!
Replace word
Hello You

Concatenation

The concatenation of String objects is as simple as an addition.

String str="Hello World!";

void setup() {
 Serial.begin(9600);
 Serial.println(str); 
 Serial.println(F("Concat word"));
 str=str+"You";
 Serial.println(str); 
 
}

void loop() {}

Hello World!
Concat word
Hello World!You

Comparison of two strings

To copy and compare strings, the usual operators “=” and “==” can be used.

String str="Hello World!";
String str1;
void setup() {
 Serial.begin(9600);
 str1=str; //copy String
 
 if(str==str1){
   Serial.println("str and str1 are the same");
 }
}

void loop() {}

Hello World!
str and str1 are the same

Functions to remember

  • sizeof(str) to know the size of the array
  • length(str) to know the length of a string in an array
  • replace(str, str1) replace str with str1
  • substring(index1, index2) extract the String from index1 to index2
  • indexOf(), lastIndexOf() to find a character in the String

Working with char arrays and strings

In some projects, you will likely have to manipulate both types of variables. It is possible to switch from one to the other by using the String() constructor to switch from char to String and by using the toCharArray() function to switch from String to char.

String str="Hello World!",str1;
char chars[]="Hello World!",chars1[20];

void setup() {
 Serial.begin(9600);
 str1=String(chars); //convert char to String
 str.toCharArray(chars1, 20);
 
 if(str==str1){
   Serial.println("str and str1 are the same");
 }

  if(strcmp(chars,chars1)==0){
   Serial.println("chars and chars1 are the same");
 }
}

void loop() {}

Sources