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
The String class, part of the core as of version 0019, 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.
22
-
23
-
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.
21
+
Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including:
22
+
23
+
* a constant string of characters, in double quotes (i.e. a char array)
24
+
* a single constant character, in single quotes
25
+
* another instance of the String object
26
+
* a constant integer or long integer
27
+
* a constant integer or long integer, using a specified base
28
+
* an integer or long integer variable
29
+
* an integer or long integer variable, using a specified base
30
+
* a float or double, using a specified decimal palces
31
+
32
+
Constructing a String from a number results in a string that contains the ASCII representation of that number. The default is base ten, so
33
+
[source,arduino]
34
+
----
35
+
String thisString = String(13);
36
+
----
37
+
gives you the String "13". You can use other bases, however. For example,
38
+
39
+
40
+
[source,arduino]
41
+
----
42
+
String thisString = String(13, HEX);
43
+
----
44
+
45
+
gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary,
46
+
47
+
[source,arduino]
48
+
----
49
+
String thisString = String(13, BIN);
50
+
----
51
+
52
+
gives you the String "1101", which is the binary representation of 13.
24
53
[%hardbreaks]
25
54
26
55
27
56
[float]
28
-
=== Functions
57
+
=== Syntax
58
+
[source,arduino]
59
+
----
60
+
String(val)
61
+
String(val, base)
62
+
String(val, decimalPlaces)
63
+
----
64
+
65
+
[float]
66
+
=== Parameters
67
+
`val`: a variable to format as a String - *Allowed data types:* string, char, byte, int, long, unsigned int, unsigned long, float, double
68
+
69
+
[float]
70
+
=== Returns
71
+
an instance of the String class.
72
+
73
+
--
74
+
// OVERVIEW SECTION ENDS
75
+
76
+
77
+
78
+
// HOW TO USE SECTION STARTS
79
+
[#howtouse]
80
+
--
81
+
82
+
[float]
83
+
=== Example Code
84
+
All of the following are valid declarations for Strings.
85
+
[source,arduino]
86
+
----
87
+
String stringOne = "Hello String"; // using a constant String
88
+
String stringOne = String('a'); // converting a constant char into a String
89
+
String stringTwo = String("This is a string"); // converting a constant string into a String object
90
+
String stringOne = String(stringTwo + " with more"); // concatenating two strings
91
+
String stringOne = String(13); // using a constant integer
92
+
String stringOne = String(analogRead(0), DEC); // using an int and a base
93
+
String stringOne = String(45, HEX); // using an int and a base (hexadecimal)
94
+
String stringOne = String(255, BIN); // using an int and a base (binary)
95
+
String stringOne = String(millis(), DEC); // using a long and a base
96
+
String stringOne = String(5.698, 3); // using a float and the decimal places
0 commit comments