Java String split() method Examples

The string split() method is used to split the string into a string array of the given regular expression.

Syntax:
public String[] split(String regex)

regex − the delimiting regular expression.

It returns the array of strings computed by splitting this string around matches of the given regular expression.

String class provides two useful methods to split a string in java.

1). public String[] split(String regex):

This java string split method is used to split the string based on the given regular expression string. The String is split and returned in the form of a string array.

This method was introduced in Java 1.4. The trailing empty strings are not included in the returned string array.

import java.io.*; public class SplitTest1 { public static void main(String args[]) { String Str = new String("id:name:dob:email:salary"); System.out.println("the splitted String is :"); System.out.println(); for (String str: Str.split(":")) { System.out.println(str); } } } output: the splitted String is : id name dob email salary

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

It is used when we want the string to be split into a limited number of strings. We can get the result of the first method bypassing the limit as 0.

public String[] split(String regex) { return split(regex, 0); }

import java.io.*; public class SplitTest2 { public static void main(String args[]) { String Str = new String("id:name:dob:email:salary"); String s = "Sara,Mumbai,IN"; String[] data = s.split(",", 2); System.out.println("Name = " + data[0]); System.out.println("Address = " + data[1]); } } output: Name = Sara Address = Mumbai,IN

3). Java String split with multiple delimiters (special characters)

we can pass multiple delimiters while using the split() method. In this example, 

We are splitting the input string based on multiple special characters.
import java.io.*; public class SplitTest3 { public static void main(String args[]) { String s = " ,Bala;Mumbai,Sara;London#Vishnu$Berlin"; String[] str = s.split("[,;#$]"); System.out.println("Substrings count is : " + str.length); for (int i = 0; i < str.length; i++) { System.out.println(str[i]); } } } output: Substrings count is : 7 Bala Mumbai Sara London Vishnu Berlin

4). Splitting String based on whitespace

import java.io.*; public class SplitTest4 { public static void main(String args[]) { String message = "Hello World,Welcome to Java World!!"; String[] strArr = message.split(" "); for (String str: strArr) System.out.println(str); } } output: Hello World,Welcome to Java World!!

Important points about String split

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such an empty leading substring.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 time, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Post/Questions related to Java String split() method Examples

Java String Copy Examples
String lastIndexOf() method examples in Java
String indexOf() method example in Java
String array in Java

In this article, we have seen Java String split() method Examples.