You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Text Strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a String out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the link:../stringobject[String object] page.
20
+
Text strings can be represented in two ways. you can use the String data type, which is part of the core as of version 0019, or you can make a string out of an array of type char and null-terminate it. This page described the latter method. For more details on the String object, which gives you more functionality at the cost of more memory, see the link:../stringobject[String object] page.
21
21
[%hardbreaks]
22
22
23
23
[float]
24
24
=== Syntax
25
-
All of the following are valid declarations for Strings.
25
+
All of the following are valid declarations for strings.
@@ -31,30 +31,30 @@ All of the following are valid declarations for Strings.
31
31
`char Str5[8] = "arduino";` +
32
32
`char Str6[15] = "arduino";`
33
33
34
-
*Possibilities for declaring Strings*
34
+
*Possibilities for declaring strings*
35
35
36
36
* Declare an array of chars without initializing it as in Str1
37
37
* Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
38
38
* Explicitly add the null character, Str3
39
-
* Initialize with a String constant in quotation marks; the compiler will size the array to fit the String constant and a terminating null character, Str4
40
-
* Initialize the array with an explicit size and String constant, Str5
41
-
* Initialize the array, leaving extra space for a larger String, Str6
39
+
* Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and a terminating null character, Str4
40
+
* Initialize the array with an explicit size and string constant, Str5
41
+
* Initialize the array, leaving extra space for a larger string, Str6
42
42
43
43
*Null termination*
44
44
45
-
Generally, Strings are terminated with a null character (ASCII code 0). This allows functions (like `Serial.print()`) to tell where the end of a String is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the String.
45
+
Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like `Serial.print()`) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren't actually part of the string.
46
46
47
-
This means that your String needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled with a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
47
+
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though "arduino" is only seven - the last position is automatically filled with a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we've explicitly included the null character (written '\0') ourselves.
48
48
49
-
Note that it's possible to have a String without a final null character (e.g. if you had specified the length of Str2 as seven instead of eight). This will break most functions that use Strings, so you shouldn't do it intentionally. If you notice something behaving strangely (operating on characters not in the String), however, this could be the problem.
49
+
Note that it's possible to have a string without a final null character (e.g. if you had specified the length of Str2 as seven instead of eight). This will break most functions that use strings, so you shouldn't do it intentionally. If you notice something behaving strangely (operating on characters not in the string), however, this could be the problem.
50
50
51
51
*Single quotes or double quotes?*
52
52
53
53
Strings are always defined inside double quotes ("Abc") and characters are always defined inside single quotes('A').
54
54
55
-
*Wrapping long Strings*
55
+
*Wrapping long strings*
56
56
57
-
You can wrap long Strings like this:
57
+
You can wrap long strings like this:
58
58
59
59
[source,arduino]
60
60
----
@@ -63,9 +63,9 @@ char myString[] = "This is the first line"
63
63
" etcetera";
64
64
----
65
65
66
-
*Arrays of Strings*
66
+
*Arrays of strings*
67
67
68
-
It is often convenient, when working with large amounts of text, such as a project with an LCD display, to setup an array of Strings. Because Strings themselves are arrays, this is in actually an example of a two-dimensional array.
68
+
It is often convenient, when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is in actually an example of a two-dimensional array.
69
69
70
70
In the code below, the asterisk after the datatype `char` "`char*`" indicates that this is an array of "`pointers`". All array names are actually pointers, so this is required to make an array of arrays. Pointers are one of the more esoteric parts of C for beginners to understand, but it isn't necessary to understand pointers in detail to use them effectively here.
71
71
@@ -86,8 +86,8 @@ In the code below, the asterisk after the datatype `char` "`char*`" indicates th
86
86
87
87
[source,arduino]
88
88
----
89
-
char* myStrings[]={"This is String 1", "This is String 2", "This is String 3",
90
-
"This is String 4", "This is String 5","This is String 6"};
89
+
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
90
+
"This is string 4", "This is string 5","This is string 6"};
0 commit comments