Java String split() method is used to split the string into a string array based on the provided regular expression. We can pass a limit to the number of elements in the returned array.

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 whole string is split and returned in the form of a string array. This method was introduced in Java 1.4. Notice that the trailing empty strings are not included in the returned string array. Let’s look at a simple java string split example to understand this. 

 

String s = "bala";
System.out.println(Arrays.toString(s.split("a")));

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

This java string split method is used when we want the string to be split into a limited number of strings. For example, let’s say we have a String variable that contains name and address with comma as a delimiter. Now, the address can have commas in them, so we can use this method.

String s = "India,New Delhi,110001";
String[] data = s.split(",", 2); System.out.println("Name = "+data[0]); //India System.out.println("Address = "+data[1])
//SplitByComma
public class SplitByComma { public static void main(String args[]) { String str = "Amol,George,Anna,David,Shubham,Patrick "; String[] arr = str.split(","); for (String a : arr) System.out.println(a); } } output: Amol George Anna David Shubham Patrick
//SplitBySpace
public class SplitBySpace { public static void main(String args[]) { String str = "Hello world Welcome to java programming!!!"; String[] arr = str.split(" "); for (String s : arr) System.out.println(s); } } output: Hello world Welcome to java programming!!!
//SplitByColon
public class SplitByColon { public static void main(String args[]) { String str = "Hellow:world:Welcome:to:java:programming!!!"; String[] arr = str.split(":"); for (String s : arr) System.out.println(s); } } output: Hello world Welcome to java programming!!!
Apache's common lang package provides a StringUtils class, which contains a null-safe split() method, that splits using whitespace as the default delimiter.

 String[] splitted = StringUtils.split("hello world"); 

Furthermore, it ignores extra spaces: 
 String[] splitted = StringUtils.split("hello    world");

Java String split important points 

1). 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.

2). If the regex doesn’t match any part of the input string then the resulting array has just one element, namely this string.

3). 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.

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

5). 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.

That’s all about string split in java.