Java – Split String

To split string in Java with a delimiter or a regular expression, you can use String.split().

Sometimes, in your Java application, you may receive some values or may need to send some values as a single string. Ofcourse these values may be defined by a specific length or mostly separated by a delimiter or separator. This separator could be a string constant or a regular expression.

In this tutorial, we will learn how to split a given string around the delimiter into a list of small pieces.

Syntax

Following is the syntax of String.split() method.

public String[] split(String regex)

All the parts of this string that match with regex (regular expression) are considered as separators. The separators are removed and the remaining parts are returned as String array.

You can also specify maximum number of splits you would like to apply on the given string. In that case, the syntax of split() function is given below.

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

Limit specifies the maximum number of splits that we can apply on the given string.

Example 1 – Split String

Following is a basic example to split a string with some string constant as a separator.

Example.java

import java.util.Arrays;

/**
 * Java Example Program to Split a String
 */

public class Example {

	public static void main(String[] args) {
		//two strings
		String str = "aba-cdc-abc";
		String separator = "-";
		
		//split string
		String[] splits = str.split(separator);
		
		System.out.print(Arrays.toString(splits));
	}
}

Run the program.

[aba, cdc, abc]

Following is the explanation how we split the string.

aba-cdc-abc      //string
    -   -         //separators
______________
 aba cdc abc      //splits
______________

Result = [aba, cdc, abc]

Example 2 – Split String – Regular Expression

In this example, we shall take a regular expression for separator of items in the string.

The separator we are taking in this example is "[abc]-". This means separator could be "a-", "b-" or "c-".

Example.java

import java.util.Arrays;

/**
 * Java Example Program to Split a String
 */

public class Example {

	public static void main(String[] args) {
		//two strings
		String str = "aba-cdc-abc";
		//regular expression for separator
		String separator = "[abc]-";
		
		//split string
		String[] splits = str.split(separator);
		
		System.out.print(Arrays.toString(splits));
	}
}

Run the program.

[ab, cd, abc]

Following is the explanation how we split the string using regular expression.

aba-cdc-abc      //string
   a-  c-         //separators
______________
 ab  cd  abc      //splits
______________

Result = [ab, cd, abc]

Example 3 – Split String – Limit Number of Splits

Now, let us limit the number of splits. To limit the number of splits, we use the second form of split() function mentioned in the syntax section.

In the following example, we shall split the string into a maximum of 4 splits.

Example.java

import java.util.Arrays;

/**
 * Java Example Program to Split a String
 */

public class Example {

	public static void main(String[] args) {
		//two strings
		String str = "ab-cd-ab-ps-ai-rp-ao";
		//regular expression for separator
		String separator = "-";
		
		//split string
		String[] splits = str.split(separator, 4);
		
		System.out.print(Arrays.toString(splits));
	}
}

Run the program.

[ab, cd, ab, ps-ai-rp-ao]

Since we mentioned the number of splits, the output splits array contain only four items, though the fourth item has separators in it.

Once the specified maximum number of splits is done, the split operation for the rest of the string is stopped and returned as the last item.

Conclusion

Concluding this Java Tutorial, we have learned how to split a string into array of items based on a string constant separator or regular expression as separator. Also, we have learned how to limit the number of splits.