Unlike the other String
methods featured on the AP CS A Exam, usage of split
is explicitly limited. The split
method accepts a regular expression as a parameter. Regular expressions are not featured on the AP CS A Exam and are rarely covered in introductory programming courses.
In AP CS A materials, split
is shown as String[] split(String del)
. In Java documentation, split
is shown as String[] split(String regex)
.
Calls to the split
method on the AP CS A Exam will be with basic strings as delimeters, such as
" "
(a space)","
", "
(a common followed by a space)"fish"
not with regular expressions.
When writing code that calls split
it is important to avoid accidentally passing a regular expression as an argument when the intent is to pass a basic string.
split
example 1 (works)
String str = "Brandon Brenda Evan";
String[] parts = str.split(" ");
After the code segment above executes, parts
contains:
["Brandon", "Brenda", "Evan"]
The split
method extracts the individual strings "Brandon"
, "Brenda"
, and "Evan"
from the implicit parameter ("Brandon Brenda Evan"
). The method extracts strings separated by a space (" "
) (a space) because the space is passed as an argument.
The split
method returns a String
array (String[]
) containing the extracted strings.
split
example 2 (works)
String str = "Brandon Horn, Brenda Horn, Evan Monster";
String[] parts = str.split(", ");
After the code segment above executes, parts
contains:
["Brandon Horn", "Brenda Horn", "Evan Monster"]
The argument is a comma followed by a space (", "
). This is treated as a basic string with no special meaning.
split
example with accidental regular expression
String str = "Brandon.Brenda.Evan";
String[] parts = str.split(".");
After the code segment above executes, parts
is an empty array ([]
).
The programmer may have intended for the call to split
to return ["Brandon", "Brenda", "Evan"]
but it does not.
Many characters have special meaning when included in a regular expression, including a dot ("."
).
Calling split
Each character in the string "<([{\^-=$!|]})?*+.>"
has a special meaning when used in a regular expression. A backslash followed by a character (ex: "\d"
) also has special meaning.
Avoid including these in strings passed as arguments to split
(unless your intent is to write a regular expression).
Additional split
examples
The examples below demonstrate specific behaviors that may not be obvious. The AP CS A Exam is unlikely to trick students with respect to the behavior shown in these examples; however, students may encounter these in their own code.
With a word
String str = "onefishtwofishredfishbluefish";
String[] parts = str.split("fish");
After the code segment above executes, parts
contains:
["one", "two", "red", "blue"]
With spaces
String str = "one fish two fish red fish blue fish";
String[] parts = str.split("fish");
After the code segment above executes, parts
contains:
["one ", " two ", " red ", " blue "]
Note the spaces surrounding each extracted string, except the first.
With spaces 2
String str = "one fish two fish red fish blue fish";
String[] parts = str.split(" fish ");
After the code segment above executes, parts
contains:
["one", "two", "red", "blue fish"]
Note the last extracted string.
With delimeter at beginning
String str = " Brandon Brenda Evan ";
String[] names = str.split(" ");
After the code segment above executes, names
contains:
["", "Brandon", "Brenda", "Evan"]
Note the empty string (""
) at the beginning and the lack of one at the end.
Documentation
The 2025 AP CS A Course Description documents split
as used on the AP CS A Exam (PDF page 114, page number 107 on bottom).
The Java API documentation for split(String) references the Java API documentation for split(String, int). The 2 parameter split
method is not featured on the AP CS A Exam. The Java API documentation describes functionality that is not used on the AP CS A Exam.
Additional resources
- Strings on the AP CS A Exam
- compareTo on the AP CS A Exam
- The Pattern class documentation (Oracle) includes a summary of regular expression constructs.
Help & comments
Get help from AP CS Tutor Brandon Horn