You can split a String by whitespaces or tabs in Java by using the split() method of java.lang.String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces. Though this is not as straightforward as it seems, especially if you are not coding in Java regularly. Input String may contain leading and trailing spaces, it may contain multiple white spaces between words and words may also be separated by tabs. Your solution needs to take care of all these conditions if you just want words and no empty String.
In this article, I am going to show you a couple of examples to demonstrate how you can split String in Java by space. By splitting I mean getting individual words as a String array or ArrayList of String, whatever you need.
In this Java String tutorial, I’ll show you three ways to split a String where words are separated by whitespaces or tabs using JDK and without using any third-party library like Apache commons or Google Guava. The first example is your ideal situation where each word in the String is separated by just one whitespace.
In the second example, you will learn how to deal with multiple whitespaces or tabs by using a greedy regular expression in Java e.g. «\s+» which will find more than whitespaces, and in the third example, you will learn how to deal with leading and trailing whitespaces on input String. You can even combine all these things in one solution depending upon your requirements.
And, If you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
1st Example — Splitting String where words are separated by regular whitespace
This is the simplest case. Usually, words are separated by just one white space between them. In order to split it and get the array of words, just call the split() method on input String, passing a space as regular expression i.e.» «, this will match a single white space and split the string accordingly.
String lineOfCurrencies = "USD JPY AUD SGD HKD CAD CHF GBP EURO INR"; String[] currencies = lineOfCurrencies.split(" "); System.out.println("input string words separated by whitespace: " + lineOfCurrencies); System.out.println("output string: " + Arrays.toString(currencies)); Output: input string words separated by whitespace: USD JPY AUD SGD HKD CAD CHF GBP EURO INR output string: [USD, JPY, AUD, SGD, HKD, CAD, CHF, GBP, EURO, INR]
This is also the easiest way to convert the String to String array, but If you want to convert the String array into ArrayList of String you can see the Java How to Program by Dietel, one of the most comprehensive books for beginner and intermediate Java programmers.
2nd Example — Splitting String where words are separated by multiple whitespaces or tabs
In order to handle this scenario, you need to use a greedy regular expression, which will match any number of white spaces. You can use «\s+» regex for this purpose. If you look closely, we are using regular expression metacharacters and character classes. s will match any space including tabs but require escaping hence it becomes \s, but it’s not greedy yet.
So we added +, which will match 1 or more occurrences, so it becomes greedy. To learn more about «\s+» regular expression to remove white spaces, see this tutorial.
Anyway here is the code in action:
String lineOfPhonesWithMultipleWhiteSpace = "iPhone Galaxy Lumia"; String[] phones = lineOfPhonesWithMultipleWhiteSpace.split("\s+"); System.out.println("input string separated by tabs: " + lineOfPhonesWithMultipleWhiteSpace); System.out.println("output string: " + Arrays.toString(phones)); Output: input string separated by tabs: iPhone Galaxy Lumia output string: [iPhone, Galaxy, Lumia]
You can see how we have converted a String to an array where three values are separated by multiple whitespaces. If you want to learn more about how regular expression works in Java, I suggest you read the regular expression chapter from Java: How to Program by Deitel and Deitel.
3rd Example — Splitting String with leading and trailing whitespace
Splitting a String by white space becomes tricky when your input string contains leading or trailing whitespaces because they will match the \s+ regular expression and an empty String will be inserted into the output array. To avoid that you should trim the String before splitting it i.e. call the trim() before calling split().
Though you should remember that since String is immutable in Java, you either need to hold the output of trim or chain the trim() and split() together as shown in the following example:
String linewithLeadingAndTrallingWhiteSpace = " Java C++ "; String[] languages = linewithLeadingAndTrallingWhiteSpace.split("\s"); languages = linewithLeadingAndTrallingWhiteSpace.trim().split("\s+"); System.out.println("input string: " + linewithLeadingAndTrallingWhiteSpace); System.out.println("output string wihtout trim: " + Arrays.toString(languages)); System.out.println("output string after trim() and split: " + Arrays.toString(languages)); Output: input string with leading and trailing space: Java C++ output string without trim: [, Java, C++] output string after trim() and split: [Java, C++]
If you want an ArrayList of String instead of a String array then follow the steps given in this tutorial.
Java Program to split string by spaces or tabs
Here is our sample Java program, which combines all these examples and scenarios to give you the complete idea of how to split a String by spaces in Java.
public class StringSplitExample { public static void main(String args[]) { // You can split a String by space using the split() // function of java.lang.String class. // It accepts a regular expression and you just need to // pass a regular expression which matches with space // though space could be whitespace, tab etc // also words can have multiple spaces in between // so be careful. // Suppose we have a String with currencies separated by space String lineOfCurrencies = "USD JPY AUD SGD HKD CAD CHF GBP EURO INR"; // Now, we will split this string and convert it into an array of String // we use regex " ", which will match just one whitespace String[] currencies = lineOfCurrencies.split(" "); System.out.println("input string words separated by whitespace: " + lineOfCurrencies); System.out.println("output string: " + Arrays.toString(currencies)); // above regular expression will not work as expected if you have multiple // space between two words in string, because it could pick extra // whitespace as another word. To solve this problem, we will use // a proper greedy regular expression to match any number of whitespace // they are actually separated with two tabs here String lineOfPhonesWithMultipleWhiteSpace = "iPhone Galaxy Lumia"; String[] phones = lineOfPhonesWithMultipleWhiteSpace.split("\s+"); System.out.println("input string separted by tabs: " + lineOfPhonesWithMultipleWhiteSpace); System.out.println("output string: " + Arrays.toString(phones)); // above regular expression will not able to handle leading // and trailing whitespace, as it will count empty String // as another word, as shown below String linewithLeadingAndTrallingWhiteSpace = " Java C++ "; String[] languages = linewithLeadingAndTrallingWhiteSpace.split("\s+"); System.out.println("input string with leading and traling space: " + linewithLeadingAndTrallingWhiteSpace); System.out.println("output string: " + Arrays.toString(languages)); // You can solve above problem by trimming the string before // splitting it i.e. call trim() before split() as shown below languages = linewithLeadingAndTrallingWhiteSpace.trim().split("\s+"); System.out.println("input string: " + linewithLeadingAndTrallingWhiteSpace); System.out.println("output string afte trim() and split: " + Arrays.toString(languages)); } }
This program has demonstrated all three ways which we have discussed earlier to split a String by single or multiple whitespaces or tabs in Java.
Important points about the split() method:
- Splits this string around matches of the given regular expression.
- Returns the array of strings computed by splitting this string around matches of the given regular expression
- The split() method throws PatternSyntaxException — if the regular expression’s syntax is invalid
- This method was added to Java 1.4, so it’s not available to the earlier version but you can use it in Java 5, 6, 7, or 8 because Java is backward compatible.
That’s all about how to split a String by space in Java. In this tutorial, you have learned to split a String where words are separated by a single white space, multiple whitespaces, tabs, and String containing leading and trailing whitespace. I have also shown you the trick to convert the String array you get from the split() method to convert into ArrayList of String. If you have any questions or doubts, feel free to ask.
Other Java programming tutorials on String you may like
- How to split String by comma in Java?
- How to split String using regular expressions in Java?
- 5 examples of splitting String in Java
- 2 ways to split String in Java by dot
- How to split a CSV String in Java?
- How to split String using StringTokenizer in Java?
- How to split String in JSP page using JSTL
To convert a space-separated string to a list in Python, call the str.split()
method:
sentence = "This is a test" words_list = sentence.split() print(words_list)
Output:
['This', 'is', 'a', 'test']
This works because the str.split()
splits the string by blank spaces by default.
Let’s then take a look at how to split a string of integers into a list of integers.
How Does the split() Method Work?
In Python, you can split a string into a list using the split()
method. The split()
method divides the string into substrings based on a specified separator and returns them as a list.
As an example:
string = "Hello World" list = string.split() # splits the string using whitespace as the separator print(list)
Output:
['Hello', 'World']
Here, the split()
method is called on the string
variable. It returns a list of two elements, which are "Hello"
and "World"
.
By default, the split()
method uses whitespace as the separator, but you can specify a different separator by passing it as an argument to the split()
method.
For example, if you want to split a string based on a comma separator, you can do this:
string = "apple,banana,orange" list = string.split(",") # splits the string using a comma as the separator print(list)
This results in the following output:
['apple', 'banana', 'orange']
Convert String of Integers to a List in Python
To convert a space-separated string of integers to a list in Python:
- Split the string into empty spaces.
- Convert each element to an integer.
- Add each integer to a list.
You can do this with a for loop:
numbers_str = "1 2 3 4 5" numbers_list = [] for num_str in numbers_str.split(): num_int = int(num_str) numbers_list.append(num_int) print(numbers_list)
Output:
[1, 2, 3, 4, 5]
To make the expression way shorter, you can use a list comprehension:
numbers_str = "1 2 3 4 5" numbers_list = [int(num) for num in numbers_str.split()] print(numbers_list)
Output:
[1, 2, 3, 4, 5]
Further Reading
Python Tricks
Python List Comprehensions
About the Author
-
I’m an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
I am trying to make an app that takes inputed string and then separates every character by space, and finally show it. For example, when I input «pizza» it should output «p i z z a». Unfortunately, the following code, which I wrote, does not work:
@IBOutlet var input: UITextField!
@IBOutlet var output: UITextField!
@IBAction func split(sender: AnyObject) {
I think the problem lies in the following for-in:
for character in input.text!.characters.indices {
input.text = String(input.text![character], terminator: "")
}
output.text = input.text
}
I am new to programming and I was trying to find the solution on the web, but I did not manage to. Could you help me?
You can create an array of your string characters and use joinWithSeparator to join it with a space:
extension String {
var spacedString: String {
return characters.map{String($0)}.joinWithSeparator(" ")
}
}
"pizza".spacedString
There is a space between words /each word in a sentence. | Options |
·
Next Topic
Posted: Monday, November 26, 2012 8:49:47 PM |
Joined: 7/4/2012
Posts: 8,425
Neurons: 37,889
There is a space between words in a sentence.
There is a space between each word in a sentence.
Which of the above sentences correct?
Thanks.
Posted: Monday, November 26, 2012 8:56:30 PM |
Joined: 6/15/2012
Posts: 1,469
Neurons: 3,878
Location: Australia
Hi Elaine, My personal choice is the second version.
Cheers
Posted: Monday, November 26, 2012 10:11:32 PM |
Joined: 9/10/2009
Posts: 1,647
Neurons: 4,678
I think the second sentence is more explicit.
Posted: Tuesday, November 27, 2012 12:29:04 AM |
Joined: 6/28/2011
Posts: 35
Neurons: 281
Location: San Juan, San Juan, Argentina
I agree with the above replies on the second sentence being more explicit, and there’s also a third possibility: There is a space every two words in a sentence.
Posted: Tuesday, November 27, 2012 2:49:24 AM |
Joined: 3/29/2012
Posts: 1,136
Neurons: 27,622
Location: Velké Meziříčí, Vysocina, Czech Republic
From a purely logical view, is «between each word» correct?
I think that «between» requires at least two objects.
Wouldn’t it be better and more precise to say
«There is a space after each word in a sentence.»
(but here we would have a problem with the last word)
or
«There are spaces between words in a sentence.»
(but this formulation would allow for more spaces between each pair of words.)
Posted: Tuesday, November 27, 2012 9:24:49 AM |
Joined: 6/28/2011
Posts: 35
Neurons: 281
Location: San Juan, San Juan, Argentina
papo_308 wrote:
From a purely logical view, is «between each word» correct?
I think that «between» requires at least two objects.
Wouldn’t it be better and more precise to say
«There is a space after each word in a sentence.»
(but here we would have a problem with the last word)
I think it would be better to say «There is a space between one word and the next» or «There is a space between a word and the following word.»
papo_308 wrote:
«There are spaces between words in a sentence.»
(but this formulation would allow for more spaces between each pair of words.)
I would solve it by saying «There is one space between words in a sentence.»
Posted: Tuesday, November 27, 2012 9:35:46 AM |
Joined: 10/22/2010
Posts: 21
Neurons: 63
Location: United States
Maximilian wrote:
papo_308 wrote:
From a purely logical view, is «between each word» correct?
I think that «between» requires at least two objects.
Wouldn’t it be better and more precise to say
«There is a space after each word in a sentence.»
(but here we would have a problem with the last word)
I think it would be better to say «There is a space between one word and the next» or «There is a space between a word and the following word.»
papo_308 wrote:
«There are spaces between words in a sentence.»
(but this formulation would allow for more spaces between each pair of words.)
I would solve it by saying «There is one space between words in a sentence.»
Wow, this is getting complicated even though it’s all the kind of thing we’d quickly say without much thought. How about saying, «In a sentence, a single space separates each word from the next word»?
Posted: Tuesday, November 27, 2012 3:57:31 PM |
Joined: 6/28/2011
Posts: 35
Neurons: 281
Location: San Juan, San Juan, Argentina
I think it’s all a matter of delivering a clear message so that people understand what you mean. That last option, «In a sentence, a single space separates each word from the next word,» sounds clear enough to me.