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 sub­strings. There are also some par­tic­u­lar­i­ties to note when using the method. We explain every­thing you need to know.

What is split()?

Java strings con­cate­nates char­ac­ters, digits and special char­ac­ters. They exist as objects in the class java.lang and are created and modified with the String class. In the Java pro­gram­ming language, you can also separate strings into different sub­strings. Java’s split() is used to extract certain segments from a string or to split it into in­di­vid­ual 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.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

What is the syntax of Java’s split()?

The basic syntax of Java’s split() is very simple, but can be expanded with optional pa­ra­me­ters. Here is the basic version:

String[] split(String regex)
java

In 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 rec­om­mend­ed.

The ad­di­tion­al optional parameter limit allows you to specify how many sub­strings should be created. Its value is an integer. If it is zero or negative, all sub­strings will be included in the output. If you choose a positive value, you will get that number of sub­strings in the output. With the ad­di­tion­al optional parameter limit, the syntax will look as follows:

String[] split(String regex, int limit)
java

How 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);
		}
	}
}
java

In this example, a string variable x is ini­tial­ized. 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 sub­strings. The Java command System.out.println gives us the following output:

This
is
an
example
java

How to use comma as separator, with and without spaces

You can also use split() in Java with strings con­tain­ing lists separated by commas. You’ll just need to use a little trick, because of how precisely your in­struc­tions are im­ple­ment­ed 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);
		}
	}
}
java

In this example, the output isn’t exactly perfect:

Sunday
 Monday
 Tuesday
 Wednesday
 Thursday
 Friday
 Saturday
java

The 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);
		}
	}
}
java

The output is now much cleaner:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
java

How 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);
		}
	}
}
java

Here’s the output:

194.518.552.1.17
6339
java

How 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 sub­strings 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 ex­pres­sion 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);
		}
	}
}
java

Since 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
java

split() with a limit of 0

If you set the limit parameter to 0, the regular ex­pres­sion will again be used as many times as possible. The length of the array isn’t re­strict­ed 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);
		}
	}
}
java

The output looks similar to above, but without the final string:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
java

split() with a limit greater than 0

The dif­fer­ence becomes more obvious when you set a limit greater than 0. Then it will determine how many sub­strings 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);
		}
	}
}
java

We’ll then get an output that’s sig­nif­i­cant­ly different from those above:

Sunday
Monday
Tuesday, Wednesday, Thursday, Friday, Saturday
java
Go to Main Menu