Beginner 15 min read

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
  1. String firstName = 'Amara'; — declares a String (text) variable. Apex strings use single quotes.
  2. String lastName = 'Okafor'; — same pattern, for the surname.
  3. String fullName = firstName + ' ' + lastName; — the + operator concatenates (joins) firstName, a literal space, and lastName into one new string.
  4. System.debug(fullName); — logs fullName, printing Amara 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
  1. String raw = ' Retail Customer '; — declares raw with extra spaces on either side, deliberately, to show what the methods below actually do.
  2. System.debug(raw.trim()); — calls .trim(), which returns a new string with leading/trailing whitespace removed, then logs it.
  3. System.debug(raw.toUpperCase()); — calls .toUpperCase(), returning raw with every letter capitalized. raw itself never changes — each method call returns a brand-new string.
  4. System.debug(raw.contains('Customer')); — calls .contains('Customer'), which returns a Boolean — true, since 'Customer' does appear inside raw.

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!';
  1. String customerName = 'Priya Sharma'; — the customer's name, as text.
  2. String orderNumber = '48213'; — the order number, stored as a String here (not a number) because it's only ever used as display text, never in arithmetic.
  3. 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 + lastName gives AmaraOkafor, not Amara Okafor — the space needs to be added explicitly.
  • Comparing strings with == when case might differ. 'Apex' == 'apex' is false — 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
  1. String email = 'Jane.Doe@Example.com'; — the raw input, with mixed case.
  2. 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.
  3. 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Text: String Quiz

1. What does the + operator do when used between two String values?

2. 'Apex' == 'apex' evaluates to true in Apex.

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

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.