Guide to Splitting a String by Whitespace in Java
Guide to Splitting a String by Whitespace in Java
Knowing how to split a string by whitespace in Java is a useful skill to master. This guide will show you the basics of splitting a string by spaces, tabs, commas, and more. We’ll also look at some useful methods that you can use to manipulate and convert strings.
Splitting By Spaces
The simplest way to split a string is by spaces. You can do this by using the split()
method available in the String
class. This method will return an array of strings created by splitting the original string on each occurrence of the specified delimiter:
String s = "This is a string!";
String[] words = s.split("\\s+");
for (String word : words) {
System.out.println(word);
}
// Outputs:
// This
// is
// a
// string!
The delimiter used in the example is a whitespace \\s+
. You can find more details on the different delimiters you can use in the official documentation.
Splitting By Tabs, Commas, and Other Characters
You can also split strings by other characters such as tabs, commas, pipes, and more. The same split()
method works for this task as well. You just need to specify the delimiter in the split()
method:
String s = "This,is,a,string!";
String[] words = s.split(",");
for (String word : words) {
System.out.println(word);
}
// Outputs:
// This
// is
// a
// string!
Using Regular Expressions
You can also use regular expressions to split up strings. To do this, you will need to import the Pattern
and Matcher
classes from the java.util.regex
package. Once you have these imported, you can create Pattern objects and Matcher objects with your chosen regular expression. Then you can use the find()
and group()
methods to split your string:
String s = "This is a string!";
Pattern pattern = Pattern.compile("\\w+");
Matcher matcher = pattern.matcher(s);
System.out.println("Found words:");
while (matcher.find()) {
System.out.println(matcher.group());
}
// Outputs:
// Found words:
// This
// is
// a
// string
As you can see, the find()
method returns true only when the matcher finds another occurrence of the regular expression—in this case \\w+
, which matches a word—and the group()
method returns the actual matched word. This makes it easy to iterate through the entire string and split it up into words.
Converting Strings to Arrays
Now that you know how to split a string, you might be wondering how to convert your newly-created array of strings into an array of integers. Fortunately, this is easy to do in Java. You can use the String.split()
method, which returns an array of strings, and then use the Integer.parseInt()
method to convert each string to an integer.
String s = "1 3 5 7 9";
String[] words = s.split("\\s+");
int[] numbers = new int[words.length];
for (int i = 0; i < words.length; i++) {
numbers[i] = Integer.parseInt(words[i]);
}
// Outputs:
// numbers = [1, 3, 5, 7, 9]
You can use a similar approach to convert strings to double, float, boolean, and more. Just use the appropriate method for the type you want to convert to.
Conclusion
In this guide, we showed you the basics of how to split a string by whitespace in Java. We looked at how to use the split()
method and how to use regular expressions and the Pattern
and Matcher
classes. Finally, we showed you how to convert an array of strings into an array of integers.