Java split string by space

Java Split String by Space

In this post, we will see how to split String by space in java.

In Java, we can split or break a string using the split() method. There are two split methods in Java, and their basic syntax is as follows:

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

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.";

The Output is:

[Good, night;, good, night!, parting, is, such, sweet, sorrow;, That, I, shall, say, good, night, till, it, be, morrow.]

The second example shows how to use a whitespace regex to do the same.

This regex identifies and delimits based on single whitespace.

[Good, night;, good, night!, parting, is, such, sweet, sorrow;, That, I, shall, say, good, night, till, it, be, morrow.]

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:

The Output is:

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]

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

Output:

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]

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:

Output:

The Output of split :[, All, that, glitters, is, not, gold]

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.

Output:

Input String : All that glitters is not gold
The Output of split :[All, that, glitters, is, not, gold]

Output:

Input String :These violent delights have violent ends…
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.

Output:

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.]

Java split string by space into Array Program

Given below are the entire program and the Output of the same for your reference.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *