The strings are made up of different characters in Java. Each character combines in a certain sequence to create a string. The first and foremost step in the creation of the string is to create an object for the string and then use these created objects in the code. In Java Strings once created can not be changed. These strings are utilized according to different scenarios in which different functions and methods are used on the declared string. One such case is to split/break a string in Java.
In this article, different methods are used to split the string in Java.
How to Split a String in Java?
To split a string in Java there are different methods used. These methods can be implemented according to the requirements of the program. One of the most common and widely used methods is the split) method that splits the strings from the specified regex. Below are different methods to break a string in Java
- split() Method
- Split Tokenizer class
- Pattern class
Method 1: The split() Function in Java
The split() method in Java breaks the string from the specified regular expression. This method consists of two variants. One implementation is shown below to get an overview of the topic.
public class SplitMethod1 {
public static void main(String args[])
{
String s = "Java,is,easy,and,fun,to,learn";
String[] arr1 = s.split(",");
for (String x : arr1) System.out.println(x);
}
}
In the above Java code:
- The class is declared as “PlitMethod1”.
- The subsequent step involves the declaration of string “s”.
- The array of a string is split using the regex “,”.
- The for loop loops through the array of strings and splits from the specified regex.
Output
The output below shows that the strings are split from the specified Regex.

Method 2: Use SplitTokenizer Class in Java
The SplitTokenizer class in Java splits the strings into tokens or substrings based on the delimiter. This class by default takes whitespaces as delimiters as shown in the code below.
import java.util.StringTokenizer;
public class SplitExample2 {
public static void main(String[] args) {
//Declare the string
String content = "Java is a Programming Language";
//use of StringTokenizer class of Java
StringTokenizer token = new StringTokenizer(content);
//Use the while loop
while (token.hasMoreTokens()) {
String str = token.nextToken();
System.out.println(str);
}
}
}
In the above code block:
- The string object is declared as “content”.
- The token object is declared as the tokenizer class.
- Next, the hasMoreTokens() checks if there are any other tokes to split and returns if they are present otherwise false.
- The nextToken() method of the Tokenizer class returns the next token object present.
Output
In the below output, the string is split into tokens according to the whitespaces.

Method 3: Pattern Class to Split String in Java
This method is used to create a pattern from the regular expression which is passed as the parameter to the declared method. This method is used along with the split() method to break the string in Java as depicted in the code below.
import java.util.regex.Pattern;
public class SplitMethod3 {
private static String limit = ",";
private static String content = "Java,is,easy,and,fun,to,learn";
public static void main(String[] args) {
Pattern p = Pattern.compile(limit);
String[] output = p.split(content);
for (String result: output) {
System.out.println(result);
}
}
}
In the above code:
- The util.regex.Pattern package is imported.
- The first string is declared as “limit” which contains the matching pattern.
- The second string is declared as “content”.
- In the next step, the pattern.compile() method compiles the pattern of the declared string.
- The split method splits the strings around the matching pattern and the output is printed using the println() method.
Output
The output below shows that the string is split around the declared regex that is “,”.

This sums up the implementation of different methods to split a string in Java
Conclusion
To split a string in Java different methods are utilized according to the need. These methods are the split() methods, the methods of the split Tokenizer class, and the Pattern class. Among these methods, the most common and effective method is the string.split() method in Java. This article has implemented all the methods in detail.