String Object Constructors
Initialize string objects.
The String object allows you to manipulate strings of text in a variety of useful ways. You can append characters to Strings, combine Strings through concatenation, get the length of a String, search and replace substrings, and more. This tutorial shows you how to initialize String objects.
1String stringOne = "Hello String"; // using a constant String2String stringOne = String('a'); // converting a constant char into a String3String stringTwo = String("This is a string"); // converting a constant string into a String object4String stringOne = String(stringTwo + " with more"); // concatenating two strings5String stringOne = String(13); // using a constant integer6String stringOne = String(analogRead(0), DEC); // using an int and a base7String stringOne = String(45, HEX); // using an int and a base (hexadecimal)8String stringOne = String(255, BIN); // using an int and a base (binary)9String stringOne = String(millis(), DEC); // using a long and a base10String stringOne = String(5.698, 3); // using a float and the decimal places
All of these methods are valid ways to declare a String object. They all result in an object containing a string of characters that can be manipulated using any of the String methods. To see them in action, upload the code below onto an Arduino board and open the Arduino IDE serial monitor. You'll see the results of each declaration. Compare what's printed by each println() to the declaration above it.
Hardware Required
- Arduino Board
Circuit
There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open.
Code
Learn more
You can find more basic tutorials in the built-in examples section.
You can also explore the language reference, a detailed collection of the Arduino programming language.
Last revision 2015/08/11 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.