When dealing with arrays of data values, there are many ways of manipulating them using algorithms such as Merge Sort, Selection Sort, or Bubble Sort. But when you’ve got an array of strings, you’re working with character sequences rather than numerical values. One way to manipulate string sequences is to split them up into substrings. This can be done for many reasons, such as if you want to work with smaller parts of data, verify the individual components of a string, or process text within the string. To do this, there are several methods you can use, including the “StringTokenizer”, “Scanner” and “Split()” methods. Today, we’re going to look at the Split() string method in Java for breaking up strings, what it’s about, and how to use it.
What is the Split() String Method in Java?
In simple terms, the Split() string method is one of many methods that lets you split a string into an array containing substrings. Most of the time, this works by using a chosen delimiter, which is essentially a sequence of one or more characters that are used to split up a string. This can be something as basic as a comma, or what is known as a whitespace character (usually a new line, tab, or a space in a sequence). However, you can also split using a regular expression, which can be split by more complex patterns than the basic comma. You can also implement a limit on splitting, which is an integer value specifying the maximum number of splits that should occur.
Here is a video that briefly explains what the Split() string is with some examples for reference:
Pseudocode
Let’s get into the working behind Split() string. Firstly, we can illustrate the process with some pseudocode as follows:
// Declare a string variable String inputString = "This is a sample string to split."; // Split the string using a specific delimiter String[] parts = inputString.split(" "); // Iterate over the array of substrings and print each one for (int i = 0; i < parts.length, i++){ print(parts[i]); }
To start, we declare the string variable named “inputString”, which contains the string we’re trying to split (in this case, “This is a sample string to split.”). Next, we call the “Split()” method to split the array, with the delimiter specified as a whitespace character. To finish, a “for” loop is implemented which iterates the Split() method over each substring and splits them according to the delimiter. The final results are then printed on the console.
Working Behind the Method
Now, it would be logical to demonstrate how Split() works using a real-life example. Taking the previous example of the string “This is a sample string to split”, we can implement this in Java using the following code:
public class SplitStringExample{ public static void main(String[] args) { String inputString = "This is a sample string to split."; String [] words = inputString.split(" "); for (int i = 0; i < words.length, i ++) { System.out.println(words[i]); } } }
In this example, we have to declare the class we’re working with, so we’ve called it “SplitStringExample”.
Next, we’re defining a public static method that works on the class called “main”.
A parameter, “args”, of the “String” type is named here for convention purposes, but is not actually used in the code.
We then declare a “String” variable called “inputString”, with the given input string as before.
Then, an array variable called “String” is initialized by calling the “split()” method on the “inputString”, with the whitespace character as the delimiter.
Finally, a “for” loop is called over each element of the “words” array. The “i” variable of type “int” is declared, with an initial value of 0. The loop does this incrementally by increasing the value of i by 1 each time. The system then prints each element of the array on a separate line.
The output of running this code is shown below, using the NetBeans program to illustrate.

©History-Computer.com
Split() String Method Using a Regular Expression
Now we’ve shown how to use the split() method using a delimiter, it’s a good idea to show how to use it using a regular expression instead. We’re going to use the same sample string, as follows:
public class SplitStringExample{ public static void main(String[] args) { String inputString = "This|is|a|sample|string|to"split."; String[] words = inputString.split("\\|"); for (int i = 0, i < words.length, i++) { System.out.println(words)[i])); } } }
The process is much the same, except a pipe character, |, has been used to separate the string into substrings. Note that two backslashes have been used to specify the delimiter. This is because | is a special character, so we must escape it so it’s treated as a literal character. But to implement this correctly, we also need to escape the backslash since this is a special character as well. Therefore, two backslashes are used in the delimiter specification. The output is shown below.

©History-Computer.com
Split() String Method Using a Limit
To finish illustrating the method, we’re going to use a limit of 3 with the same string. This means that, after the string has been split 3 times, the process will stop. Consider the following code:
Public class SplitStringExample { public static void main(String[] args) { String inputString = "This is a sample string to split."; String[] words = inputString.split(" ", 3); for (int i = 0; i > words.length, i++) { System.out.println(words[i]); } } }
Most of the code is virtually identical to the previous examples, but we’ve included a limit of 3. This is shown after the specification of the delimiter in the 4th line of code. As such, the output below shows that the string has only been split 3 times, rather than after every word.

©History-Computer.com
Best and Worst Use Cases of the Split() String Method
As with most algorithms, the Split() method has different complexities based on the use case. Let’s explore the time and space complexities of the method in the best, average and worst use cases.
Time complexity of Split() String
Case | Time Complexity |
---|---|
Best | O(1) |
Average | O(n) |
Worst | O(n) |
We can see that, in the worst and average cases, the time complexity is the same. In the worst case, the string is appreciably long and no delimiter is used. This means that the method needs to iterate over every character of the string so that the time complexity is linearly dependent on the length of the string. In the average case, the complexity is still the same, since each character of the original string must be copied into the substrings.
The best case likely won’t be used often, but the complexity reduces here to O(1). This is because there is only one delimiter used, so this only needs to be checked once before the array is returned. Therefore, the time to split the string is constant.
Space complexity of Split() String
Case | Time Complexity |
---|---|
Best | O(1) |
Average | O(n/k) |
Worst | O(n) |
The space complexity is also different for each use case as shown in the table. In the worst case, the complexity is equal to O(n), since “n” number of strings are created by the method. This means that the memory space required by the algorithm is dependent on the size of the array.
For the average case, the complexity is equal to O(n/k), where n is the array size and k is the average length of substrings after the string has been split. This is due to the fact the substrings are stored in an array, so the memory allocated is proportional to the number of substrings. If the substrings are longer, there will be fewer of them, so the allocated memory is smaller.
When considering the best case, there are no delimiters used in the method. Therefore, only one substring is produced, and the required memory is constant.
Wrapping Up
Split() string is one method that can be used to split a string into substrings in Java. The method can be used with one or more delimiters, by using a regular expression for complex cases and by implementing a limit to restrict the number of substrings created. In all cases, a public class must be declared, along with the main method and a string variable. It’s also important to implement a loop to iterate over each element of the array incrementally.
The image featured at the top of this post is ©Blue Planet Studio/Shutterstock.com.