Apex Method of the Day – String myString.split(String regExp)

We can split a String into parts using a token, which in this case is a space:

String str = 'Foo Bar Force';
List<String> parts = str.split(' ');
system.assertEquals(3, parts.size());
system.assertEquals('Foo', parts[0]);
system.assertEquals('Bar', parts[1]);
system.assertEquals('Force', parts[2]);

However, the following example, using “.” as a token will fail:

String str = 'Foo.Bar.Force';
List<String> parts = str.split('.');
system.assertEquals(3, parts.size());

System.AssertException: Assertion Failed: Expected: 3, Actual: 0

This is because the token is actually a regular expression, and some characters, like “.” have a special meaning within a regular expression. These special characters will need to be escaped with a backslash if they are to be treated literally – as is our intention.

However, by adding a backslash the following will actually fail to compile:

String str = 'Foo.Bar.Force';
List<String> parts = str.split('\.');
system.assertEquals(3, parts.size());

Invalid string literal ‘\.’. Illegal character sequence ‘\.’ in string literal

Backslash itself is a special character in Apex String literals, and so it needs to be further escaped with an additional backslash. Finally, the following gives the desired result:

String str = 'Foo.Bar.Force';
List<String> parts = str.split('\\.');
system.assertEquals(3, parts.size());

You have to think of this as a two stage process. In the first stage Apex interprets the literal string, in the second stage the regular expression is evaluated from the results of the first stage.

The Apex Code Developer’s Guide provides an interesting example where we want to split the string using the backslash character itself. The backslash is escaped – giving us 2 – and then each backslash also is escaped – giving us 4:

List<String> parts = filename.split('\\\\');

See also:
Force.com Apex Code Developer’s Guide – String Methods
Salesforce StackExchange – Bug in String.split(‘.’)?

This entry was posted in Documentation and tagged , . Bookmark the permalink.

2 Responses to Apex Method of the Day – String myString.split(String regExp)

  1. BIDTravel says:

    Perfectly explained and clear, thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s