Sub topic 6
Strings
This chapter assumes you can declare variables and know the primitive types from earlier chapters. You should also be able to run anonymous Apex in the Developer Console.
What a String is
A String holds text. Literals use single quotes. To put a single quote inside a literal, escape it with a backslash.
String greeting = 'Hello';
String quoted = 'It\'s Apex';
System.debug(greeting + ', ' + quoted);The + operator joins strings. Strings are immutable, so a method never changes the original. It returns a new string, and you must keep the result.
String s = 'apex';
s.toUpperCase();
System.debug(s);
s = s.toUpperCase();
System.debug(s);The first debug prints apex. The second prints APEX.
Common methods
| Method | Purpose |
|---|---|
| `length()` | Number of characters |
| `contains('x')` | True if the substring is present |
| `substring(0, 3)` | Characters from index 0 up to, not including, 3 |
| `indexOf('x')` | Position of the first match, or -1 |
| `trim()` | Removes leading and trailing whitespace |
| `split(',')` | Returns a `List<String>` |
String csv = 'red, green ,blue';
List<String> parts = csv.split(',');
for (String p : parts) {
System.debug('[' + p.trim() + ']');
}
System.debug(String.join(parts, '|'));Comparing strings
The == operator ignores case in Apex. The equals method is case-sensitive. equalsIgnoreCase matches == in behavior but states the intent clearly.
String a = 'apex';
String b = 'APEX';
System.debug(a == b);
System.debug(a.equals(b));
System.debug(a.equalsIgnoreCase(b));This prints true, false, true. Use equals when case matters, such as when you compare record IDs written as text.
Empty, blank, and null
A string can be null, empty (''), or only whitespace. Calling a method on a null string throws a NullPointerException. The String class has static helpers that handle null safely.
String note = null;
System.debug(String.isBlank(note));
System.debug(String.isBlank(' '));
System.debug(String.isEmpty(' '));This prints true, true, false. isBlank is true for null, empty, or whitespace-only text. isEmpty is true only for null or ''. Prefer isBlank when you validate user input.
Building text with format
For longer messages, String.format fills numbered placeholders. This is easier to read than many + operators.
String msg = String.format('{0} has {1} open cases', new List<Object>{ 'Acme', 4 });
System.debug(msg);Common errors
- Forgetting to assign the result of a method such as
trim()ortoLowerCase(). - Using double quotes. Apex only accepts single quotes for string literals.
- Assuming
==is case-sensitive, as it is in Java. - Calling a method on a string that may be null.
Exercise
Given String code = 'ab12'; what do code == 'AB12' and code.equals('AB12') return, and what does String.isEmpty(' ') return?
Show answer
code == 'AB12' returns true because == ignores case in Apex. code.equals('AB12') returns false because equals is case-sensitive. String.isEmpty(' ') returns false because the string has one space and is neither null nor ''.