split() String Method in Java with Examples

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The split() in Java is a method that is used to break a string around the matches of the provided regular expression. The split() in Java is also used to break a string based on the provided string delimiter. The split() method in Java is invoked by a string and returns an array of strings.

What is String Split() in Java?

Before learning more about the split() in java, and examples, let us first learn about the strings in java.

A string is a sequence of characters. Java considers the string as objects. We can use the Java String class to create and manipulate the strings.

Now, let's talk about how the split() method works on strings. The split in Java is a method defined under the String class, which is used to break a string around the matches of the provided regular expression. We can also say that the split in Java is used to break a string based on the provided string delimiter. The string delimiters are mostly comma (,) and full stops (.). The String.split() method returns an array of generated substrings.

Refer to the image below to see how a string is split into substrings using split() method in Java.

String Split in Java

In the example above, we can see that the string is divided into 5 substrings using the delimiter comma (,). So after splitting, we will get five substrings- "ALPHA", "BETA", "GAMA", "DELTA", and "SIGMA".

Syntax of split() in Java

There are two signature for split() method:

To divide a string into multiple substrings (split a string into substrings), we can use split() in Java accordingly. There are two syntaxes of split() in Java for two different method signatures. The first one takes the limit, and the other does not.

Syntax of split() in Java with limit provided:

Syntax of a split() in Java without limit:

We will discuss more about these two types of syntaxes later in the Types of the split() in Java section.

Parameters of String split() in Java

Now, as we have discussed the syntax of a split() in Java. Let us talk about the parameters of the split method.

The different parameters of the syntax:

  1. regex: regex or regular expression is a required delimiting parameter. We can simply say that the string is divided according to the provided regex.
  2. limit: the limit is an optional parameter. The limit is used to limit or control the number of strings generated.

Since the limit is an optional string, if the limit is not provided in the parameter, the split() will return all the possible substrings.

Note: In String. split(), the string is the method calling (function invoking) string, that we want to split into substrings.

Return Values of String split() in Java

Return Type: array of strings

A string invokes the split in Java and returns an array of strings. The array of strings consists of all the substrings generated by the split() method using the regex and limit provided.

Exception of String split() in Java

The split() method generally does not raise errors and exceptions if we use the correct syntax. However, it can raise an exception called the PatternSyntaxExpression exception when the regex passed as a parameter is invalid.

If we provide 0 or a negative integer as the length parameter, then the split() method returns an array containing all the substrings.

Example

Let's see a basic program to understand the split() method working in java. Here the given string is spilt using the delimeter +. Limit parameter is 0, it means returned array wil contain all substrings.

Output:

Types of the split() Methods in Java

So far we have seen how the split() method works in Java. Let’s learn more about the different types of split() methods in Java with their examples, return values, parameters, and exceptions.

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

One of the types of split() method takes a regular expression as well as limit.

Syntax:

Parameters

The split() method takes two parameters, namely regex and limit. The regex contains the delimiter, and the limit parameter contains the number of the strings returned after being generated.

Returns

The split() method returns an array of strings.

Throws

The split() method raises an exception called PatternSyntaxException if the regular expression provided is invalid.

Example

Output:

Note: If we need to use a special character such as \, |, ^, *, + etc., we can use an escape sequence to escape these characters. Example: We can use \\+ to escape and split using +.

b. public String[] split(String regex)

The other type of string method takes only a regular expression in the parameter.

Syntax:

Parameters

The split() method takes only regex or regular expression in the parameter. The regex contains the delimiter.

Returns

Similar to the previous split(regex, limit) method, the split(regex) method also returns an array of strings.

Throws

Similar to the previous split(regex, limit) method, the split(regex) method raises an exception called PatternSyntaxException if the regular expression provided is invalid.

Example

Output:

Advantages of Split() in Java

  • The split() in Java is used to break the string at the separator. It returns an array of substrings.
  • The split() in Java helps in parsing complex strings into the desired result using regex delimiters.

More Examples of String Split() in Java

Let us take some examples to understand the working and syntax of `split() in Java.

Example 1: Java String split() method with regex and length

Output:

As we can see in the example above, we can split the string and limit the number of terms needed. In the above example, we have set the limit = 3, so we can see that the first two substrings got divided but the rest is intact in the third substring.

Example 2: Split a String in Java with Delimiter

Let us take an example and try different length limits and see the return substrings.

Output:

In the example above, the string is split using the delimiter :.

Note: If we prove the limit to be 0, then the entire string is divided as per the delimiter, and an array of the substrings is returned.

Example 3: Split a string in Java by Space

So far, we have seen many examples of the split() method, let us divide the string using space.

Output:

Conclusion

  • The split() in Java is a method defined under the String class which is used to break a string around the matches of the provided regular expression.
  • The split() in Java is used to break a string based on the provided string delimiter. The string delimiters are mostly comma (,), and full stops (.).
  • To divide a string into multiple substrings (split strings into substrings), we can use split in Java. Syntax: string.split(String regex, int limit);
  • regex or regular expression is a required delimiting parameter. We can simply say that the string is divided according to the provided regex.
  • limit is an optional parameter. The limit is used to limit or control the number of strings generated.
  • The split() method in Java is invoked by a string and returns an array of strings. The array of strings consists of all the substrings generated by the split() method using the regex and limit provided.

:::section{.main)