Show minor edits - Show changes to markup
The String library, part of the core as of version 0019, allows you to use and manipulate strings of text in more complex ways than the string character array does. 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.
For reference, constant character arrays are referred to as strings with a small s, and String objects are referred to as Strings with a capital S.
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.
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.
All of the following are valid declarations for Strings.
String stringOne = "Hello String"; // using a constant String
String stringOne = String('a'); // converting a constant char into a String
String stringTwo = String("This is a string"); // converting a constant string into a String object
String stringOne = String(stringTwo + " with more"); // concatenating two strings
String stringOne = String(13); // using a constant integer
String stringOne = String(analogRead(0), DEC); // using an int and a base
String stringOne = String(45, HEX); // using an int and a base (hexadecimal)
String stringOne = String(255, BIN); // using an int and a base (binary)
String stringOne = String(millis(), DEC); // using a long and a base
Possible ways to declare strings
Constant integer Strings
Declaring a String with a constant number results in a string that contains the ASCII representation of that number. The default is base ten,so
String thisString = String(13)
gives you the String "13". You can use other bases, however. For example,
String thisString = String(13, HEX)
gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary,
String thisString = String(13, BIN)
gives you the String "1011", which is the binary representation of 13.
Experienced programmers may be wondering how to declare a String of a fixed length of 13 characters. There's no constructor for that, because Strings can be added to through concatenation, or shortened through using trim().
Examples:
The String library, part of the core as of version 0019, allows you to use and manipulate strings of text in more complex ways than the string character array does. 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.
For reference, constant character arrays are referred to as strings with a small s, and String objects are referred to as Strings with a capital S.
All of the following are valid declarations for Strings.
String stringOne = "Hello String"; // using a constant String
String stringOne = String('a'); // converting a constant char into a String
String stringTwo = String("This is a string"); // converting a constant string into a String object
String stringOne = String(stringTwo + " with more"); // concatenating two strings
String stringOne = String(13); // using a constant integer
String stringOne = String(analogRead(0), DEC); // using an int and a base
String stringOne = String(45, HEX); // using an int and a base (hexadecimal)
String stringOne = String(255, BIN); // using an int and a base (binary)
String stringOne = String(millis(), DEC); // using a long and a base
Possible ways to declare strings
Constant integer Strings
Declaring a String with a constant number results in a string that contains the ASCII representation of that number. The default is base ten,so
String thisString = String(13)
gives you the String "13". You can use other bases, however. For example,
String thisString = String(13, HEX)
gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary,
String thisString = String(13, BIN)
gives you the String "1011", which is the binary representation of 13.
Experienced programmers may be wondering how to declare a String of a fixed length of 13 characters. There's no constructor for that, because Strings can be added to through concatenation, or shortened through using trim().
Examples: