Beginner 15 min read

String Concatenation and Formatting

By the end of this lesson, you'll be able to:

  • Build a message by concatenating strings and variables
  • Write the minimal List<String>{...} shape String.format() requires
  • Use String.format() to build a message more readably
  • Explain when formatting is preferable to plain concatenation

Prerequisites: "Type Conversion vs Casting"

Concatenation, revisited

String name = 'Thabo';
Integer orderNumber = 4821;
String message = 'Hi ' + name + ', your order #' + orderNumber + ' is confirmed.';
  1. String name = 'Thabo'; — the customer's name.
  2. Integer orderNumber = 4821; — the order number, as a real number this time.
  3. String message = ... — builds the sentence with +, the same concatenation from Module 3.

This works, but gets harder to read as more variables join in — every + is a small interruption to the sentence you're building.

A quick necessary detour: writing a List of values

String.format() needs its placeholder values grouped together as a single list, written like this:

new List<String>{ name, String.valueOf(orderNumber) }
  1. new List<String>{ name, String.valueOf(orderNumber) } — breaking this one line into its three parts:
    • new creates the list itself — you'll see new again for creating other kinds of values later in this course; here, it's simply "make a new list."
    • List<String> means "a list that holds String values."
    • { name, String.valueOf(orderNumber) } is the list's actual contents, comma-separated, in order.

That's the entire shape you need for now — a full, dedicated lesson on lists (adding items, reading them back, looping over them) comes in Module 7. Here, just recognize new List<String>{ ... } as "the values String.format() slots into the message," in the exact order they appear.

String.format() as an alternative

String message = String.format(
    'Hi {0}, your order #{1} is confirmed.',
    new List<String>{ name, String.valueOf(orderNumber) }
);
  1. String message = String.format( — calls String.format(), opening its argument list, which spans the next two lines.
  2. 'Hi {0}, your order #{1} is confirmed.', — the message template. {0} and {1} are placeholders.
  3. new List<String>{ name, String.valueOf(orderNumber) } — the values that fill those placeholders, in order: {0} becomes name, {1} becomes the converted orderNumber.
  4. ); — closes the call.

Note that String.format() expects String values in that list — non-string values (like our Integer) need converting first, using exactly the String.valueOf() you met last lesson.

A real business example: Retail

An order-confirmation message with several dynamic parts — customer name, order number, delivery date — is a natural fit for String.format(): the sentence structure stays visible and readable, instead of getting broken up by + operators every few words.

Trade-offs and when NOT to use this

For a simple, one- or two-variable message, plain + concatenation is often just as readable and involves less ceremony (no building a List). String.format() earns its keep once you have three or more variables, or when the same message template gets reused in multiple places.

Exercise

Using String.format(), build a message: 'Case #{caseNumber} was closed after {days} days.' — with caseNumber '00012345' and days 3.

Show hint

Remember: String.format() needs a List<String>, and days needs converting first with String.valueOf().

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

String Concatenation and Formatting Quiz

1. What must the values passed to String.format() be?

2. In new List<String>{ a, b }, what does new do?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Beyond the + operator from Module 3, Apex offers String.format() for building messages with placeholders — often more readable than a long chain of + operators, especially once several variables are involved.