What is Java split method?

The split method breaks the specified string by the given regular expression delimiter and returns an array. The array contains substrings of the specified string after splitting.

For example, you may require breaking the phone numbers that are in that format:

123-345-7898

Similarly, strings to break where comma occurs etc.

Full string split example with limit parameter example

Syntax of split Java method

Following are the ways of using the split method:

split(String regex)

  • In this way of using the split method, the complete string will be broken.
  • The regex (regular expression) defines the pattern. It can be a character like space, comma, complex expression with special characters etc. You may learn more about regex constructs.
  • The split function returns an array of broken strings that you may manipulate just like the normal array in Java.

You may also use this method as follows:

split(String regex, int limit)

That is, limit the number of splits in the given string by using the limit argument. The limit is given as int.

See the section below for the examples with code and output.

An example of completely breaking the string by split method

For demonstrating the Java string split method, a string object is created and assigned a value. In the split method, the delimiter used for breaking the string is a space character. As no limit parameter is provided, so complete string will break:

See online demo and code

Java split

You see, the string is broken into four pieces. The for loop is used to iterate through the returned array for displaying its items.

Note: You may also use \\s regex for the space character.

Using the string split with limit parameter example

Suppose, you want to break the phone numbers that are in string format as follows:

456-434-8987

You just need to break this from first dash character while keeping the second intact. This is where limit parameter can play its role. Otherwise, simply using the split method will break the string completely; in that case, in three pieces. See the code below:

See online demo and code

string split

Using dot regex for splitting a string example

Similarly, you may use dot (.) as the character where you want to break the given string. Suppose, you have long paragraphs and the requirement is to break the paragraphs into sentences based on full stops. Here is how you may do this:

See online demo and code

string split dot

Note: the simple dot will not work. You have to escape the dot in order to break the string paragraphs based on full stop.

That means:

str_split.split(“\\. “) will work.

str_split.split(“. “) will not work.

A little complex regex for multiple delimiters demo

In some scenarios, you may need to split a string for multiple delimiters. For example, break the string with space, commas, hyphens, or other characters in one call. As such, the Java split method performs split based on given regular expression, so you may create such a regex that may accommodate all.

See this example where I will split the string with space, comma, dot, and dash by using regex:

 

The output of the above code is:

Note: The “|” represents OR in regex.

Similarly, you may create your own regex as per the requirement of the project.