Text: String
By the end of this lesson, you'll be able to:
- Create and assign String variables
- Join strings together using concatenation
- Use at least two built-in String methods
Prerequisites: "Numbers"
Creating and joining strings
String firstName = 'Amara';
String lastName = 'Okafor';
String fullName = firstName + ' ' + lastName;
System.debug(fullName); // Amara Okafor
String firstName = 'Amara';— declares aString(text) variable. Apex strings use single quotes.String lastName = 'Okafor';— same pattern, for the surname.String fullName = firstName + ' ' + lastName;— the+operator concatenates (joins)firstName, a literal space, andlastNameinto one new string.System.debug(fullName);— logsfullName, printingAmara Okafor.
Useful built-in methods
A String variable comes with methods you call using dot notation:
String raw = ' Retail Customer ';
System.debug(raw.trim()); // 'Retail Customer' — whitespace removed
System.debug(raw.toUpperCase()); // ' RETAIL CUSTOMER '
System.debug(raw.contains('Customer')); // true
String raw = ' Retail Customer ';— declaresrawwith extra spaces on either side, deliberately, to show what the methods below actually do.System.debug(raw.trim());— calls.trim(), which returns a new string with leading/trailing whitespace removed, then logs it.System.debug(raw.toUpperCase());— calls.toUpperCase(), returningrawwith every letter capitalized.rawitself never changes — each method call returns a brand-new string.System.debug(raw.contains('Customer'));— calls.contains('Customer'), which returns aBoolean—true, since'Customer'does appear insideraw.
A real business example: Retail
A retail order confirmation might build a message like:
String customerName = 'Priya Sharma';
String orderNumber = '48213';
String message = 'Hi ' + customerName + ', your order #' + orderNumber + ' has shipped!';
String customerName = 'Priya Sharma';— the customer's name, as text.String orderNumber = '48213';— the order number, stored as aStringhere (not a number) because it's only ever used as display text, never in arithmetic.String message = ...— builds one final message by concatenating five pieces together with+: a greeting, the name, more text, the order number, and a closing phrase.
This exact pattern — concatenating variables into a human-readable message — shows up constantly in real Apex code, from email bodies to debug logs to error messages.
Common mistakes
- Forgetting spaces when concatenating.
firstName + lastNamegivesAmaraOkafor, notAmara Okafor— the space needs to be added explicitly. - Comparing strings with
==when case might differ.'Apex' == 'apex'isfalse— use.equalsIgnoreCase()when case shouldn't matter.
String methods in practice
String email = 'Jane.Doe@Example.com';
String normalized = email.toLowerCase().trim();
System.debug(normalized); // jane.doe@example.com
String email = 'Jane.Doe@Example.com';— the raw input, with mixed case.String normalized = email.toLowerCase().trim();— chains two methods:.toLowerCase()runs first and returns a lowercase copy, then.trim()runs on that result, removing any surrounding whitespace.System.debug(normalized);— logs the cleaned-up result:jane.doe@example.com.
Exercise
Declare a String greeting with extra spaces around it, like ' hello '. Debug its trimmed, uppercase version in one chained expression.
Show hint
You can chain .trim().toUpperCase() directly onto the variable.
Text: String 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
String is Apex's text type — anything in quotes. Strings come with a rich set of built-in methods for common tasks like changing case, trimming whitespace, and checking contents, which you'll reach for constantly.