Before diving into the main topic which is the split() method in Java let us first understand what strings are in Java. In Java, Strings are considered as the sequence of defined characters. The Java string class is used for the creation of new strings to be used in different functions. The string class consists of various methods and one of the methods is the split() method.
In this write-up, we will implement the string split() method in detail.
How to Implement Java String split() Method?
The string split() method of Java breaks the string at the regex and the limit if provided. Each portion of the array is delimited using the regex passed or the delimited value. The most common delimiters that are used by the string split() method are full stop (.), double forward stroke(\\), and commas(,).
The syntax of the split() method is depicted below using two different method:
strings.split(String regex);
The above method is declared without limit.
string.split(String regex, int limit);
The above method contains regex with a limit.
Let us have a look at the examples below to get a firm understanding of the split() method.
Example 1: Implementation of split() Method Using Regex
The example below depicts the implementation of the split() method using regex.
//Declare the class for the split() method
class splitmethod{
//The Main class in Java
public static void main(String args[]){
//Declare the string that is to be split
String p1="How to implement the split method in Java";
//The split() method that uses whitespaces for splitting.
String[] splits=p1.split("\\s");
//Using for loop to print the output
for(String x:splits){
//The println() method for printing the output
System.out.println(x);
}
}
}
In the above Java code:
- A class named “splitmethod” is declared for splitting the provided input.
- String p1 contains the string that is to be split.
- The split() method uses the whitespaces to split the words.
- The for loop contains the argument to split the string.
- The println() prints the output of the split method.
Output
The output below contains the strings printed on a separate line in a split form using the split() method.

Example 2: Using Regex and Limit Constrain
The below code shows the implementation of the split() method using regular expression and the limit.
//Declare the class for the split method.
class splitmethod{
//The main class of Java
public static void main(String args[]){
//Enter the string to split the words.
String p1="Implementation of split method in Java";
System.out.println("THE RETURNED WORDS ARE:");
//for loop for the split() method.
for(String str:p1.split("\\s",0)){
System.out.println(str);
}
//This block of code prints the entire line.
System.out.println("THE RETURNED WORDS ARE:");
for(String str:p1.split("\\s",1)){
System.out.println(str);
}
//This code block prints the words in two parts.
System.out.println("THE RETURNED WORDS ARE:");
for(String str:p1.split("\\s",2)){
System.out.println(str);
}
}}
In the above code:
- The class for splitmethod is created.
- In the Main class, a string is declared as p1.
- The for loop declares the split method. The 0 in the for loop indicates that each and every word is printed on a separate line.
- An entire line is printed when the limit is 1.
- The output contains the words printed into two parts when the limit passed is 2.
Output
The below output contains the words that are printed through the for loop using the split() method of Java.

Conclusion
The split() string method of the String class in Java splits the string using two different syntaxes. The first syntax takes a regex only to split the string while the second syntax takes the regex and the limit and splits the string according to the specified limit. In this write-up, we have implemented the split() method in Java.