Variables & Constants

Lesson 7 - Working with String and String()

Learn how Arduino String stores text, how String() constructor creates text values, and how to combine and use strings in beginner ESP32 projects.

Progress indicator

Lesson 7 of 7

Learning Objectives

  • Understand what String means in Arduino and how it stores text.
  • Learn what the String() constructor does and when to use it.
  • Compare String with char arrays in beginner-friendly language.
  • Practice String declaration, initialization, and concatenation.
  • Use common String functions to inspect or modify text values.
  • Build readable Serial messages by combining labels and converted number values.

Concept Explanation

What is String in Arduino

String in Arduino is a data type used to store text, such as words, sentences, or messages you want to print to Serial.

Example text values include "Hello", "ESP32", and "Sensor OK".

Beginners often use String first because it makes Serial messages easier to read and write compared to lower-level text handling.

What is String() Constructor

String() is a constructor that creates a String object from another value, such as a number, character, or text literal.

It is commonly used when you want to convert values into text before combining them into a message.

String countText = String(123);
String label = String("LED");

This is especially helpful when printing mixed data like numbers + words, such as "Count: 5".

String vs char Array

A String is easier for beginners because it gives helpful text features like concatenation and functions. A char array is a lower-level way to store text and is more manual to manage.

String

Beginner-friendly, easier to read and combine text.

char Array

More manual, useful later when learning C-style strings and memory control.

Beginner rule: use String first for learning and readability. Learn char arrays later when you want deeper memory control.

String Syntax

Basic syntax:

String variableName;
String name;
String mode = "Auto";
String text = String(123);

String (capital S) is the Arduino text class. Writing string (lowercase s) is a common beginner mistake.

Declaring String Variables

Declaring means creating a variable and choosing String as the type so it can hold text.

String deviceName;
String statusMessage;

Initializing String

Initializing means giving a starting text value.

String boardName = "ESP32";
String mode = "Ready";

Initializing text clearly makes debugging easier because you know what the program should print before any updates happen.

Creating String with String()

Use String() when you want to turn numbers or other values into text. This is very useful for Serial messages.

int count = 5;
String text = String(count);   // "5"

Important idea

5 is a number, but "5" is text. String(5) converts the number into text so it can be joined with other words.

String Concatenation

Concatenation means joining text together. In Arduino String, the + operator is commonly used.

String name = "ESP32";
String msg = "Board: " + name;

When combining numbers and text, String(number) helps convert the number to text first.

This is called building a message dynamically, which is very common in sensor projects and Serial debugging.

Common String Functions

Arduino String has useful functions for checking and modifying text.

text.length()
text.toUpperCase()
text.toLowerCase()
text.substring(0, 3)
text.indexOf("A")

Example uses: checking message length, converting case, or finding characters.

length()

Counts how many characters are in the text.

substring()

Extracts part of the text (useful for parsing).

When to Use String

  • Serial debug messages and labels
  • UI/menu text on displays
  • Combining text with sensor values for readable output
  • Beginner learning projects where readability matters most

For larger advanced projects, you may later learn more memory-efficient text strategies. For beginner lessons, String is a practical and readable choice.

Real-Life Example

A temperature monitor may build and print text like "Room Temp: 28 C" by joining a label and a converted number using String().

Example Code

This example shows how to create text with String and String() before printing messages.

String boardName = "ESP32";
String statusText = String("Ready");
int blinkCount = 3;

void setup() {
  Serial.begin(115200);

  String message = String("Board: ") + boardName;
  String countText = String("Blink count = ") + String(blinkCount);

  Serial.println(message);
  Serial.println(statusText);
  Serial.println(countText);
}

void loop() {
  // Lesson focus is text creation in setup()
}

What this example teaches

  • How to declare and initialize String variables
  • How to create Strings using String()
  • How to concatenate text and converted numbers
  • How to print readable messages to Serial

Example Code Explanation

  1. String boardName = "ESP32"; creates a text variable containing the board name.
  2. String statusText = String("Ready"); creates a String using the constructor.
  3. int blinkCount = 3; stores a number that we later convert to text.
  4. String message = String("Board: ") + boardName; joins two text values.
  5. String countText = String("Blink count = ") + String(blinkCount); converts the number to text, then joins it into one readable sentence.
  6. Serial.println(...) prints the final messages to the Serial Monitor.

Beginner reading tip

When reading String code, find these 3 things first: source values (like boardName), converted values (like String(blinkCount)), and final message variables (like message or countText).

Real-life example

A sensor dashboard might build text like "Temp: 28 C" by joining labels and converted number values using String().

Common Mistakes with String

  • Mixing up String (type) and string (wrong lowercase spelling).
  • Trying to concatenate numbers and text without converting numbers when needed.
  • Confusing String objects with single characters (char).
  • Using too many temporary Strings in larger projects without thinking about memory usage.
  • Expecting String text values to work like numbers in math operations.

Best Practices for String

  • Use String for beginner-friendly readable messages and labels.
  • Use String() to convert numbers to text before concatenation.
  • Use clear variable names like statusText, message, and countText.
  • For advanced memory control later, learn char arrays/C-style strings step by step.
  • Build messages in small readable steps (create label, convert value, combine text).

Practice Task

  1. Change boardName from "ESP32" to your own board label.
  2. Add a new String message like "Status: " + statusText.
  3. Change blinkCount and observe how String(blinkCount) changes the printed text.
  4. Try one String function like toUpperCase() on a message and explain what changes.

Try it now

Open the simulator workspace and practice reading Serial output while stepping through examples.

Run in Simulator