Split a String on any whitespace character in Java
This post will discuss how to split a string on any whitespace character in Java.
A character is called a whitespace character in Java if and only if the Character.isWhitespace(char) method returns true. The most commonly used whitespace characters are ' ', '\t', '\n', '\r' and 'f'. There are several ways to split a string on whitespace characters:
1. Using String.split() method
The standard solution to split a string is using the split() method provided by the String class. It accepts a regular expression as a delimiter and returns a string array. To split on any whitespace character, you can use the predefined character class \s that represents a whitespace character.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Hello World"; String[] words = str.split("\\s"); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
Or using POSIX character class \p{Space}.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Hello World"; String[] words = str.split("\\p{Space}"); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
To group consecutive whitespaces as a single delimiter, you can use greedy quantifier \s+ where + represents one or more times.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Hello World"; String[] words = str.split("\\s+"); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
If your string contains the leading or trailing spaces, trim the string before calling the split() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String str = " Hello World"; String[] words = str.trim().split("\\s+"); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
2. Using Pattern.compile() method
If the regular expression is frequently called, you might want to compile the regular expression for performance boost:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.regex.Pattern; public class Main { private static final Pattern SPACE = Pattern.compile("\\s+"); public static void main(String[] args) { String str = " Hello World"; String[] words = SPACE.split(str.trim()); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
3. Using StringUtils class
We can also achieve this using the split() method from the StringUtils class provided by the Apache Commons library. This effectively handles leading and trailing spaces, and adjacent delimiters are also treated as one.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; public class Main { public static void main(String[] args) { String str = " Hello World"; String[] words = StringUtils.split(str); System.out.println(Arrays.toString(words)); } } |
Output:
[Hello, World]
4. Using StringTokenizer class
The StringTokenizer class allows breaking a string into tokens using the default delimiter set, which consists of the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. The usage of this class is discouraged.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { String str = " Hello World"; StringTokenizer result = new StringTokenizer(str); List<String> tokens = new ArrayList<>(); while (result.hasMoreTokens()) { tokens.add(result.nextToken()); } System.out.println(tokens); } } |
Output:
[Hello, World]
That’s all about splitting a string on any whitespace character in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)