String split()
Java String.split() Method
Last Updated : 17 Mar 2025
The split() method is defined in the String class. It splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
Syntax:
Parameters: The method accepts a delimiting regular expression as a parameter.
Returns: The method returns a string computed by splitting this string around matches of the given regular expression.
Exception: The method throws PatternSyntaxException if the regular expression's syntax is invalid.
There is another method signature of the split() method.
limit: limit for the number of strings in array. If it is zero, it will return all the strings matching regex.
Internal Implementation
Example 1: Java String.split() Method
The given example returns total number of words in a string excluding space only. It also includes special characters.
Output:
java string split method by TpointTech
Example 2: Java String.split() Method
Output:
returning words: welcome to split world returning words: welcome to split world returning words: welcome to split world
Example 3: Java String.split() Method
Here, we are passing split limit as a second argument to this function. This limits the number of splitted strings.
Output:
Returning words: Tpoin Tech Split array length: 2
Advantages
- Easy to Use: The method is simple and intuitive - just provide a delimiter, and it splits the string into an array.
- Flexible Delimiters (Regex Support): It uses regular expressions so that you can split on complex patterns (e.g., whitespace, multiple symbols).
- Built-in and Efficient: No need for extra libraries - it's part of core Java (java.lang.String) and optimized for performance.
- Works with Limit Parameter: We can control the number of splits with the limit argument, which helps keep parts of the string intact if needed.
Disadvantages
- Regex Complexity: The delimiter is treated as a regex, which can be confusing for beginners (for example, splitting by. needs "\\.").
- Creates Extra Empty Strings: When splitting strings with consecutive delimiters, it may produce empty strings in the result array.
- Less Control for Advanced Parsing: Not ideal for complex tokenization (e.g., quoted strings, escape characters) - you might need a custom parser or StringTokenizer.
- Not Memory-Efficient for Large Strings: It returns a new array each time, so for very large text, it could increase memory usage.
Java String.split() Method MCQs
Q1. Which of the following is the correct return type of the String split() method?
- String
- String[]
- List
- char[]
Answer: B)
Explanation: The split() method returns an array of String objects rather than a List or a single String after splitting the string around matches of the given regular expression.
Q2. What happens if the regex passed to split() does not match any part of the string?
- It returns null
- It throws an exception
- It returns the original string in an array
- It returns an empty array
Answer: C)
Explanation: The split() method ensures no data loss by returning an array with the original string as its single element if the pattern does not match anywhere.
Q3. What will "apple, banana, orange".split(",").length return?
- 2
- 1
- 3
- 0
Answer: C)
Explanation: The length is three because the split(",") creates an array with ["apple", "banana", and "orange"] after splitting the string at each comma.
Q4. Which regular expression should you use to split a string by any whitespace?
- " "
- "\w"
- "\s+"
- "."
Answer: C)
Explanation: Words can be separated by spaces, tabs, or multiple spaces with split("\s+") since the regex \s+ matches one or more whitespace characters.
Q5. What happens if the limit parameter in split(regex, limit) is negative?
- It throws an error
- It works like limit = 0
- It splits as many times as possible
- It returns an empty array
Answer: C)
Explanation: String.split() will apply the pattern as many times as feasible and include trailing empty strings in the output if a negative limit is supplied.