Site icon AranaCorp

Summary of strings with Arduino

4.5
(19)

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

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:

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





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:

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

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

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 19

No votes so far! Be the first to rate this post.

Exit mobile version