
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: To avoid that you should trim the String before splitting it i.e. 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.
#JAVA REGEX WHITESPACE HOW TO#
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.ģrd Example - Splitting String with leading and trailing whitespace You can see how we have converted a String to an array where three values are separated by multiple whitespaces. Output : input string separated by tabs : iPhone Galaxy Lumia ( "input string separated by tabs: " + lineOfPhonesWithMultipleWhiteSpace) String phones = lineOfPhonesWithMultipleWhiteSpace. String lineOfPhonesWithMultipleWhiteSpace = "iPhone Galaxy Lumia" To learn more about "\\s+" regular expression to remove white spaces, see this tutorial. So we added +, which will match 1 or more occurrences, so it becomes greedy. \s will match any space including tabs but \ require escaping hence it becomes \\s, but it's not greedy yet. If you look closely, we are using regular expression metacharacters and character classes. You can use "\\s+" regex for this purpose. In order to handle this scenario, you need to use a greedy regular expression, which will match any number of white spaces. 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.Ģnd Example - Splitting String where words are separated by multiple whitespaces or tabs

Output : input string words separated by whitespace : USD JPY AUD SGD HKD CAD ( "input string words separated by whitespace: " String lineOfCurrencies = "USD JPY AUD SGD HKD CAD CHF GBP EURO INR" " ", this will match a single white space and split the string accordingly. 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. Usually, words are separated by just one white space between them.

This is one of the best and up-to-date courses to learn Java online.ġst Example - Splitting String where words are separated by regular whitespace You can even combine all these things in one solution depending upon your requirements.Īnd, If you are new to the Java world then I also recommend you go through The Complete Java MasterClasson Udemy to learn Java in a better and more structured way.

"\\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. 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. The first example is your ideal situation where each word in the String is separated by just one whitespace. 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. By splitting I mean getting individual words as a String array or ArrayList of String, whatever you need. In this article, I am going to show you a couple of examples to demonstrate how you can split String in Java by space. Your solution needs to take care of all these conditions if you just want words and no empty String. Input String may contain leading and trailing spaces, it may contain multiple white spaces between words and words may also be separated by tabs. Though this is not as straightforward as it seems, especially if you are not coding in Java regularly. 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. You can split a String by whitespaces or tabs in Java by using the split() method of class.
