String functions

Updated on 2 December 2021
dev board Arduino UNO
firmware Arduino
chip ATmega328P
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Buy the components

Code

Download code string-functions.ino
String sample;
String animal = "lion";

void setup() {
  Serial.begin(9600);
  Serial.println("String functions...");

  Serial.print("string.length(): ");
  Serial.println(animal.length());
}

void loop() {
  temperature(sample);

  Serial.print("Temperature returned: ");
  Serial.println(sample);

  delay(1000);
}

void temperature(String &value) {
  value = String(random(0, 30));
}

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/references *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

Prototype

A photo of the actual setup.

String functions prototype

Serial console

Serial output from the firmware.

String functions serial console

Description

Examples of various String functions and how to pass the function a reference to a String object that you want to write to.

References