In this post, we will see how to split String by space in java.
Table of Contents
split()
method. There are two split methods in Java, and their basic syntax is as follows:
1 2 3 |
<String>.split(String regex, int limit) |
Where,
regex
= Java splits the string based on this delimiter,limit
= number of substrings returned. By default, the limit is 0, and the function returns all the possible substrings.
And
1 2 3 |
<String>.split(String regex) |
Where regex
= delimiter to split string.
Both the above functions return a String array and also throw the PatternMatchException
.
We can split the String object in a variety of ways using regex expression. However, in this article, we focus on how to split String by space as delimiter.
Split String by Single Whitespace
To split a string using whitespace as a delimiter, we pass a single space character in the regex parameter. We can also use a regex pattern for identifying single whitespaces.
The first example is using a space as the delimiter. The commas in the original sentence have been changed to semicolons for clarity.
String inputStr = "Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow.";
1 2 3 4 |
// Split with space String[] result = inputStr.split(" "); |
The Output is:
The second example shows how to use a whitespace regex to do the same.
1 2 3 4 |
// Split a string with a regex for whitespace String[] resultRegex = inputStr.split("\\s"); |
This regex identifies and delimits based on single whitespace.
Further reading:
Split String by multiple whitespaces
If there are multiple whitespace characters in between different words, it is a much better idea to use regex patterns. The regex pattern: \\s
identifies single whitespace. So using this pattern to split a String with Multispaces results in something like this:
1 2 3 4 |
String inputStrMultiSpaces = "To be or not to be ,that is the question"; String[] resultMultiSpaceIncorrect = inputStrMultiSpaces.split("\\s"); |
The Output is:
Output of split :[To, be, , , , , , , , or, not, , , , , , , to, be, ,that, is, the, question]
To avoid this, we add a +
in end of the pattern. The +
allows checking for one or more whitespace occurrences. The example would now be
1 2 3 4 |
String inputStrMultiSpaces = "To be or not to be ,that is the question"; String[] resultMultiSpace = inputStrMultiSpaces.split("\\s+"); |
Output:
Output of split :[To, be, or, not, to, be, ,that, is, the, question]
If the string contains tab characters in between, we can also use the \\t+
pattern. However, it is advisable to use the earlier pattern as it covers all the scenarios.
Split String with Leading and Trailing Whitespaces by space
If a String has Leading and Trailing whitespaces and uses the above regex pattern of \\s+
, we can get some extra unwanted characters in the Output. The error is caused because Java considers whitespace as a character as well.
For example, if we have a string similar to one in the below example and we split using the pattern, the Output we get is:
1 2 3 4 |
String inputLeadingWhitespace = " All that glitters is not gold"; String[] resultLeadingWhitespaceIncorrect = inputLeadingWhitespace.split("\\s+"); |
Output:
As we can see above, an additional empty String
is right at the start of the Output, which is not correct.
The easiest way to handle this problem is to trim the string before splitting it. Trailing spaces do not pose a massive problem since the string ends, but it is advisable to trim such Strings.
Given below are examples of Leading and Trailing spaces.
1 2 3 4 |
String inputLeadingWhitespace = " All that glitters is not gold"; String[] resultLeadingWhitespace = inputLeadingWhitespace.trim().split("\\s+"); |
Output:
The Output of split :[All, that, glitters, is, not, gold]
1 2 3 4 |
String inputTrailingWhitespace = "These violent delights have violent ends... "; String[] resultTrailingWhitespace = inputTrailingWhitespace.split("\\s+"); |
Output:
The Output of split :[These, violent, delights, have, violent, ends…]
Split String by space with Limit parameter
If we add a positive number in a limit
parameter of the split()
method, Java splits the string only for those many occurrences. The rest of the string is returned as-is.
For example, if we consider the String "Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow." And give the limit as 5.
The split()
function splits the words: "good, night; good night" and returns the rest of the string as-is.
1 2 3 4 5 |
String inputStr = "Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow."; String[] resultLimit = inputStr.split(" ",5); |
Output:
Output of split :[Good, night;, good, night!, parting is such sweet sorrow; That I shall say good night till it be morrow.]
Java split string by space into Array Program
Given below are the entire program and the Output of the same for your reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package org.arpit.java2blog.entry; import java.util.Arrays; public class SplitStringBySpaceMain { public static void main(String[] args) { String inputStr = "Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow."; // Split with space System.out.println("=============================="); System.out.println("Split String by Single space"); System.out.println("=============================="); String[] result = inputStr.split(" "); System.out.println("Input String: " + inputStr); System.out.println("Output of split :" + Arrays.toString(result)); System.out.println(""); // Split a string with a regex for whitespace System.out.println("=============================="); System.out.println("Split String by whitespace regex"); System.out.println("=============================="); String[] resultRegex = inputStr.split("\\s"); System.out.println("Input String: " + inputStr); System.out.println("Output of split :" + Arrays.toString(resultRegex)); System.out.println(""); String inputStrMultiSpaces = "To be or not to be ,that is the question"; System.out.println("=============================="); System.out.println("Split String by multiple spaces in between incorrectly"); System.out.println("=============================="); // Split string with multiple spaces in between incorrectly. String[] resultMultiSpaceIncorrect = inputStrMultiSpaces.split("\\s"); System.out.println("Input String :" + inputStrMultiSpaces); System.out.println("Output of split :" + Arrays.toString(resultMultiSpaceIncorrect)); System.out.println(""); System.out.println("=============================="); System.out.println("Split String by multiple spaces in between correctly"); System.out.println("=============================="); // Split string with multiple spaces in between correctly. String[] resultMultiSpace = inputStrMultiSpaces.split("\\s+"); System.out.println("Input String :" + inputStrMultiSpaces); System.out.println("Output of split :" + Arrays.toString(resultMultiSpace)); System.out.println(""); System.out.println("=============================="); System.out.println("Split String by tab character"); System.out.println("=============================="); String inputTabs = "These violent delights have violent ends..."; String[] resultTabs = inputTabs.split("\\t+"); System.out.println("Input String :" + inputTabs); System.out.println("Output of split :" + Arrays.toString(resultTabs)); System.out.println(""); System.out.println("=============================="); System.out.println("Split String by space with leading space"); System.out.println("=============================="); String inputLeadingWhitespace = " All that glitters is not gold"; String[] resultLeadingWhitespaceIncorrect = inputLeadingWhitespace.split("\\s+"); System.out.println("Input String :" + inputStrMultiSpaces); System.out.println("The Output of split :" + Arrays.toString(resultLeadingWhitespaceIncorrect)); System.out.println(""); System.out.println("=============================="); System.out.println("Split trimmed String by space with leading space"); System.out.println("=============================="); String[] resultLeadingWhitespace = inputLeadingWhitespace.trim().split("\\s+"); System.out.println("Input String :" + inputLeadingWhitespace); System.out.println("The Output of split :" + Arrays.toString(resultLeadingWhitespace)); System.out.println(""); System.out.println("=============================="); System.out.println("Split String by space with trailing space"); System.out.println("=============================="); String inputTrailingWhitespace = "These violent delights have violent ends... "; String[] resultTrailingWhitespace = inputTrailingWhitespace.split("\\s+"); System.out.println("Input String :" + inputTrailingWhitespace); System.out.println("The Output of split :" + Arrays.toString(resultTrailingWhitespace)); System.out.println(""); // Split by space with Limit System.out.println("=============================="); System.out.println("Split String by space with limit"); System.out.println("=============================="); String[] resultLimit = inputStr.split(" ",5); System.out.println("Input String :" + inputStr); System.out.println("Output of split :" + Arrays.toString(resultLimit)); System.out.println(""); } } |
Output:
Split String by Single space
==============================
Input String: Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow.
Output of split :[Good, night;, good, night!, parting, is, such, sweet, sorrow;, That, I, shall, say, good, night, till, it, be, morrow.]
==============================
Split String by whitespace regex
==============================
Input String: Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow.
Output of split :[Good, night;, good, night!, parting, is, such, sweet, sorrow;, That, I, shall, say, good, night, till, it, be, morrow.]
==============================
Split String by multiple spaces in between incorrectly
==============================
Input String :To be or not to be ,that is the question
Output of split :[To, be, , , , , , , , or, not, , , , , , , to, be, ,that, is, the, question]
==============================
Split String by multiple spaces in between correctly
==============================
Input String :To be or not to be ,that is the question
Output of split :[To, be, or, not, to, be, ,that, is, the, question]
==============================
Split String by tab character
==============================
Input String :These violent delights have violent ends…
Output of split :[These, violent , delights, have , violent , ends…]
==============================
Split String by space with leading space
==============================
Input String :To be or not to be ,that is the question
The Output of split :[, All, that, glitters, is, not, gold]
==============================
Split trimmed String by space with leading space
==============================
Input String : All that glitters is not gold
The Output of split :[All, that, glitters, is, not, gold]
==============================
Split String by space with trailing space
==============================
Input String :These violent delights have violent ends…
The Output of split :[These, violent, delights, have, violent, ends…]
==============================
Split String by space with limit
==============================
Input String :Good night; good night! parting is such sweet sorrow; That I shall say good night till it be morrow.
Output of split :[Good, night;, good, night!, parting is such sweet sorrow; That I shall say good night till it be morrow.]
That’s all about Java split string by space.