In this Java tutorial, you will learn how to split a given string into parts by a delimiter string using String split() method, with examples.

Java – Split String

To split a string with a separator or a delimiter string, you can use split() method of String class.

Sometimes, in your Java application, you may receive some values or may need to send some values as a single string. Of course, 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. And we need to split the given string into values.

In this tutorial, you will learn how to split a given string around the delimiter into a list of values.

Syntax of split() method

The syntax of String split() method is given below.

</>
Copy
String.split(String regex, int limit)

split() method can take two arguments.

ParameterDescription
regex[Mandatory]
A String type value.
The value which is used as delimiter string or separator string.
limit[Optional]
An int type value.
Specifies the maximum number of splits to be done on the string.

Let us see what a regex is. A regex can be a plain string that can be used straight as a delimiter, or a regular expression. If regular expression is given, then all the parts of the string that match with regex (regular expression) are considered as separators.

Examples

1. Split string by a delimiter in Java

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

Example.java

</>
Copy
import java.util.Arrays;

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]

2. Split string by a Regular Expression in Java

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

</>
Copy
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]

3. Split string by limiting the number of splits in Java

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

</>
Copy
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 String method 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.