JavaDevNotes.com

Java String Split Space Or Whitespace Examples

A common programming scenario in Java is to Split Strings using space or whitespaces as separators. This is useful, for example, when we want to retrieve all the words in a text document. The whitespaces are the markers that separates each word. Split is very powerful because you can specify the delimiter via regular expression. It can save a lot of time because of the power of regex.

Split by single space

If we have the String "Apple Banana Carrot", we can see that there are 3 words separated by single spaces. Here is a sample code on how to split the String and retrieve the words:
public class TestConsole {
   public static void main(String[] args) {
      String sampleString = "Apple Banana Carrot";
      String[] animals = sampleString.split(" ");
      int animalIndex = 1;
      for (String animal : animals) {
         System.out.println(animalIndex + ". " + animal);
         animalIndex++;
      }
   }
}

Noticed that we incorporated a counter in the logic so that it is clear how many tokens were found. The output is shown below.

1. Apple
2. Banana
3. Carrot

The animal array contains three items in this case.

Split by multiple space/whitespace

Tab, space, and line feed are usually what we call whitespace. In regular expression, \s stands for a whitespace. A sequence of one or more whitespaces can be expressed as \s+. We just need to scape \ when we use in Java. Here is an example code that parses a String separated by multiple spaces.:
public class TestConsole {
   public static void main(String[] args) {
      String sampleString = "Cat  Dog    Elephant     Fox";
      String[] animals = sampleString.split("\\s+");
      int animalIndex = 1;
      for (String animal : animals) {
         System.out.println(animalIndex + ". " + animal);
         animalIndex++;
      }
   }
}

Here is the sample output. Notice that the four items were correctly identified.

1. Cat
2. Dog
3. Elephant
4. Fox

Here is a modified example that parses a String that contains tabs and line feeds:

public class TestConsole {
   public static void main(String[] args) {
      String sampleString = "Cat    Dog \t Elephant \n Fox \r\n Goat";
      String[] animals = sampleString.split("\\s+");
      int animalIndex = 1;
      for (String animal : animals) {
         System.out.println(animalIndex + ". " + animal);
         animalIndex++;
      }
   }
}

And the output is still as expected.

1. Cat
2. Dog
3. Elephant
4. Fox
5. Goat

The animal array contains five items in this case.

Split by single space/whitespace

Though rare, there are times when we want to split a String by only a single whitespace. Here is an example
public class TestConsole {
   public static void main(String[] args) {
      String sampleString = "Cat  Dog    Elephant     Fox";
      String[] animals = sampleString.split("\\s");
      int animalIndex = 1;
      for (String animal : animals) {
         System.out.println(animalIndex + ". " + animal);
         animalIndex++;
      }
   }
}

Here is a sample output. Notice that each consecutive whitespace will result to an empty String item in the resulting array.

1. Cat
2. 
3. Dog
4. 
5. 
6. 
7. Elephant
8. 
9. 
10. 
11. 
12. Fox

The animal array contains twelve items in this case, where eigth are empty Strings.


String Tutorials And Examples