How to split strings in Java
The method split()
can be used to split strings in Java. It contains a parameter for the separator and an optional parameter for the number of substrings. There are also some particularities to note when using the method. We explain everything you need to know.
What is split()
?
Java strings concatenates characters, digits and special characters. They exist as objects in the class java.lang and are created and modified with the String class. In the Java programming language, you can also separate strings into different substrings. Java’s split() is used to extract certain segments from a string or to split it into individual parts. Below we’ll show you in more detail what you can do with the method.
When splitting a Java string with split(), you’ll get an array with several strings. This becomes the return value.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
What is the syntax of Java’s split()
?
The basic syntax of Java’s split()
is very simple, but can be expanded with optional parameters. Here is the basic version:
String[] split(String regex)
javaIn this example, the output will be in the form of an array. String
is the string object. regex
is the parameter that’s used to determine where the string should be split. The parameter isn’t required but it is recommended.
The additional optional parameter limit
allows you to specify how many substrings should be created. Its value is an integer. If it is zero or negative, all substrings will be included in the output. If you choose a positive value, you will get that number of substrings in the output. With the additional optional parameter limit
, the syntax will look as follows:
String[] split(String regex, int limit)
javaHow to use split()
with space as separator
Let’s look at some practical examples. First, we’ll create a string that contains 4 words: “This is an example”. Then we’ll split up the string using split()
. The most intuitive way to split up this string is using the already existing spaces between the words. Here’s how that happens in code:
public class Main {
public static void main(String[] args) {
String x = "This is an example";
String[] output = x.split(" ");
for (String y : output) {
System.out.println(y);
}
}
}
javaIn this example, a string variable x is initialized. Then Java’s split()
is used on the string. The parameter searches the string for spaces and splits it in the places where it finds them. The result is then saved in an array called “output”. The for
loop is used to list the substrings. The Java command System.out.println
gives us the following output:
This
is
an
example
javaHow to use comma as separator, with and without spaces
You can also use split()
in Java with strings containing lists separated by commas. You’ll just need to use a little trick, because of how precisely your instructions are implemented in Java. First, let’s look at an example without the trick. Say we have a list of the days of the week, separated by commas. These commas make for the perfect separator to enter into regex
. If we set up the code this way, it will look as follows:
public class Main {
public static void main(String[] args) {
String x = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday";
String[] output = x.split(",");
for (String y : output) {
System.out.println(y);
}
}
}
javaIn this example, the output isn’t exactly perfect:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
javaThe output gives us a clear list of the days of the week. However, every value after Sunday contains a space before the day. Since we only used a comma as separator, the spaces were left there in the process of splitting. To get cleaner output, we should use both a comma and a space as separator. If we do so, the code will look as follows:
public class Main {
public static void main(String[] args) {
String x = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday";
String[] output = x.split(", ");
for (String y : output) {
System.out.println(y);
}
}
}
javaThe output is now much cleaner:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
javaHow to split Java strings with digits
You can also use split()
to split Java strings made up of digits. In our example, we’ll take a long key that is divided using periods and a colon. If we want to separate the digits after the colon, this is what the code would look like:
public class Main {
public static void main(String[] args) {
String x = "194.518.552.1.17:6339";
String[] output = x.split(":");
for (String y : output) {
System.out.println(y);
}
}
}
javaHere’s the output:
194.518.552.1.17
6339
javaHow to use split()
with the limit parameter
When splitting Java strings with split()
, you can also use the optional limit
parameter. The limit
parameter limits the number of substrings that are created. There are three options here: assigning it a value less than 0, 0 or greater than 0.
split()
with a limit less than 0
If you enter a value for limit
that’s less than 0, the regular expression will be applied without a limit on the entire string. The resulting array can be any length. That code might look as follows:
public class Main {
public static void main(String[] args) {
String x = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ";
String[] output = x.split(", ", -3);
for (String y : output) {
System.out.println(y);
}
}
}
javaSince there is a space after the last word “Saturday”, the array will end with an empty string. Here’s the output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
javasplit()
with a limit of 0
If you set the limit
parameter to 0, the regular expression will again be used as many times as possible. The length of the array isn’t restricted either. But if the last element is an empty string, it won’t be included in the resulting array.
public class Main {
public static void main(String[] args) {
String x = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ";
String[] output = x.split(", ", 0);
for (String y : output) {
System.out.println(y);
}
}
}
javaThe output looks similar to above, but without the final string:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
javasplit()
with a limit greater than 0
The difference becomes more obvious when you set a limit greater than 0. Then it will determine how many substrings can be output.
public class Main {
public static void main(String[] args) {
String x = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ";
String[] output = x.split(", ", 3);
for (String y : output) {
System.out.println(y);
}
}
}
javaWe’ll then get an output that’s significantly different from those above:
Sunday
Monday
Tuesday, Wednesday, Thursday, Friday, Saturday
java