For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
Working with strings is a daily task in Java programming. Often, you need to break a long string into smaller parts—like words, values, or tokens. That’s where the split() method comes into play. It allows developers to divide a string based on a specified delimiter, like a comma, space, or even a regular expression. Whether you're parsing user input or processing file data, this method is essential.
In this blog, you’ll learn what Java String split() is, how it works, and when to use it with multiple real-world examples.
Also, Software engineering courses allow you to explore such Java methods in depth and apply them effectively in real projects.
The split() method in Java programming divides a string into an array of substrings based on a regular expression (regex). It belongs to the String class and is extremely useful when you need to tokenize a string. Think of it like cutting a sentence into words using spaces or breaking a CSV line using commas.
It returns a string array, and the delimiter used to split can be as simple as a comma or as complex as a regex pattern.
Syntax:
String[] split(String regex)
String[] split(String regex, int limit)
Enhance your abilities through these best-in-class certifications.
This overloaded method allows you to control the number of times the string should be split. The limit parameter sets the maximum number of resulting substrings. Let’s understand Java String Split method with the help of examples:
This example shows how the method works when the limit is lower than the number of matches found.
public class SplitExample1 {
public static void main(String[] args) {
String input = "Java-is-a-popular-language";
String[] parts = input.split("-", 2);
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
Java
is-a-popular-language
Explanation: The string is split at the first hyphen. The second element holds the rest of the string because the limit is 2.
Also read: StringTokenizer Class in Java
Here, the limit is higher than the number of matches in Java String Split.
public class SplitExample2 {
public static void main(String[] args) {
String input = "One-Two-Three";
String[] parts = input.split("-", 5);
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
One
Two
Three
Explanation: The method splits the string at each hyphen. Since the limit is more than needed, it behaves like the regular split().
Java String split with a negative limit includes all trailing empty strings in the output array, ensuring no data is discarded after the final delimiter.
public class SplitExample3 {
public static void main(String[] args) {
String input = "a,,b,";
String[] parts = input.split(",", -1);
for (String part : parts) {
System.out.println("'" + part + "'");
}
}
}
Output:
'a'
''
'b'
''
Explanation: With a negative limit, all possible splits including trailing empty strings are preserved.
Also read: Top 13 String Functions in Java | Java String [With Examples]
A zero limit discards trailing empty strings.
public class SplitExample4 {
public static void main(String[] args) {
String input = "a,,b,";
String[] parts = input.split(",", 0);
for (String part : parts) {
System.out.println("'" + part + "'");
}
}
}
Output:
'a'
''
'b'
Explanation: Trailing empty substrings are removed when the limit is set to zero.
The split(String regex) method is a part of the Java String split functionality. It splits the string into an array of substrings based on the specified regular expression. It splits the original string wherever the regex matches and returns all resulting parts as separate elements in the array.
This example splits a string using a colon : as the delimiter.
public class SplitExample5 {
public static void main(String[] args) {
String input = "key:value:entry";
String[] parts = input.split(":");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
key
value
entry
Explanation: Each value is separated at the colon, producing three substrings.
Demonstrates splitting a string using a full word as a delimiter.
public class SplitExample6 {
public static void main(String[] args) {
String input = "driven by code by logic";
String[] parts = input.split("by");
for (String part : parts) {
System.out.println(part.trim());
}
}
}
Output:
driven
code
logic
Explanation:
The string is split wherever the word "by" occurs.
An empty string in regex will split between every character.
public class SplitExample7 {
public static void main(String[] args) {
String input = "ABC";
String[] parts = input.split("");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
A
B
C
Explanation:
An empty string regex breaks the input into individual characters.
Must explore: Array in Java: Types, Operations, Pros & Cons
In Java String split, the dot (.) is a special regex character and must be escaped as "\." to split by a literal dot correctly.
public class SplitExample8 {
public static void main(String[] args) {
String input = "www.google.com";
String[] parts = input.split("\\.");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
www
com
Explanation:
We use \\. to split by the dot since it's a special character in regex.
This example shows how trailing whitespace is handled.
public class SplitExample9 {
public static void main(String[] args) {
String input = "apple banana orange ";
String[] parts = input.trim().split("\\s+");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
apple
banana
orange
Explanation: The string is split using one or more spaces with \\s+, and trailing spaces are removed using trim().
In Java String split, you can use regular expressions to split strings. It can be based on complex patterns like multiple delimiters, character groups, or conditional matches for flexible parsing.
public class SplitExample10 {
public static void main(String[] args) {
String input = "abc123def456ghi";
String[] parts = input.split("\\d+");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
abc
def
ghi
Explanation: The regex \\d+ matches one or more digits. It splits the string wherever digits occur.
Must read: String Comparison in Java: Methods, Examples, and Best Practices
If the delimiter is not found, the entire string is returned as a single element.
public class SplitExample11 {
public static void main(String[] args) {
String input = "HelloWorld";
String[] parts = input.split("-");
for (String part : parts) {
System.out.println(part);
}
}
}
Output:
HelloWorld
Explanation: Since the delimiter is absent, the original string remains unchanged.
The Java String split method is widely used in real-world scenarios:
The split() method in Java is a powerful tool for breaking strings into manageable parts using regular expressions. Whether you're parsing data, extracting values, or formatting input, understanding how split() works with and without the limit parameter is essential.
Mastering its use helps write cleaner and more efficient code. To deepen your skills, consider exploring software engineering courses that focus on Java fundamentals and hands-on string manipulation techniques.
No, the split() method doesn’t throw an exception by default. However, if the regular expression syntax used as a parameter is invalid, it may throw a PatternSyntaxException. It’s important to validate the regex before using it in the method to avoid runtime errors.
No, the original string remains unchanged. Java Strings are immutable, which means any method that appears to modify a string, like split(), actually returns a new result without affecting the original string object.
The split() method returns an array of strings (String[]). This array contains substrings split around matches of the specified regular expression. You can then access each part using array indices in a loop or directly.
The limit parameter controls the number of resulting substrings. A positive value limits the number of elements. Zero removes trailing empty strings. A negative value allows all possible substrings, including trailing empty strings.
Yes, but special characters like dot (.) or pipe (|) are regex metacharacters. You must escape them using double backslashes (\\. or \\|) to split the string correctly without regex conflicts or unexpected behavior.
To split using multiple delimiters, you can use a regex pattern with the OR operator (|). For example, split(",|;|\\s") will split the string by commas, semicolons, or spaces. Always escape regex metacharacters properly.
If the delimiter or regex is not present in the string, the split() method returns an array containing the original string as the only element. It means no splitting occurred due to the absence of the pattern.
Yes, it can. If a delimiter appears at the start or end of the string, the method includes empty strings in the resulting array. To remove them, you can post-process the array or adjust the limit parameter accordingly.
When used on an empty string with any delimiter, split() returns an array with one empty string element ([""]). If the delimiter matches, it can return an array of multiple empty strings, depending on the pattern and limit.
Yes, you can convert the result from split() (a String[]) to a List using Arrays.asList(splitArray). This is helpful when you want to use collection operations like filtering, sorting, or iteration with more flexibility.
While split() is convenient, it's not always the most efficient for high-performance applications. It uses regex internally, which can be slower. For better performance, especially in large-scale parsing, consider using StringTokenizer or manual parsing with indexOf() and substring().
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.