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 String
2String stringOne = String('a'); // converting a constant char into a String
3String stringTwo = String("This is a string"); // converting a constant string into a String object
4String stringOne = String(stringTwo + " with more"); // concatenating two strings
5String stringOne = String(13); // using a constant integer
6String stringOne = String(analogRead(0), DEC); // using an int and a base
7String 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 base
10String 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.

circuit

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

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.