This course is currently a work in progress. Please join the waitlist to receive regular updates.

Substring in apex

The substring function is used to extract a part of a string from a string variable. It is a built-in function with two variations: one that requires both a start and end index, and another that only requires a start index.

H
E
L
L
O
W
O
R
L
D
The substring "WORLD" is highlighted with arrows pointing to its start and end.

Below is an example of finding a substring in apex:

Loading...

In the above example:

  • The string variable, myString has the value 'Hello World'.
  • The first substring() method extracts the characters from position 6 to 11 (‘World’). Note, the first character is at position 0.
  • The second substring() method extracts the characters from position 6 to the end of the string (‘World’).

Try It Yourself

Loading...