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

The + operator, already familiar from arithmetic, also concatenates (joins) strings together.

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

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!';

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

Methods can be chained: toLowerCase() runs first, and trim() runs on its result — a common pattern for cleaning up user input before storing or comparing it.

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.

APEX

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.

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.