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.';
String name = 'Thabo';— the customer's name.Integer orderNumber = 4821;— the order number, as a real number this time.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) }
new List<String>{ name, String.valueOf(orderNumber) }— breaking this one line into its three parts:newcreates the list itself — you'll seenewagain for creating other kinds of values later in this course; here, it's simply "make a new list."List<String>means "a list that holdsStringvalues."{ 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) }
);
String message = String.format(— callsString.format(), opening its argument list, which spans the next two lines.'Hi {0}, your order #{1} is confirmed.',— the message template.{0}and{1}are placeholders.new List<String>{ name, String.valueOf(orderNumber) }— the values that fill those placeholders, in order:{0}becomesname,{1}becomes the convertedorderNumber.);— 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().
String Concatenation and Formatting Quiz
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.