How to convert a sentence string to a string array of words in java?

Question:
How to convert a sentence string to a string array of words in java?

Answer:
String s = "This is a sample sentence.";
String[] words = s.split("\\s+");

Code:
public class ArrayOfWords {
  public static void main(String[] args) {
 
    String str = "This is an example";
    String[] words = str.split("\\s+");
 
    System.out.println("String: " + str);
 
    for(String word: words) {
      System.out.println(word);
    }
  }
}

Output:
$ java ArrayOfWords
String: This is an example
This
is
an
example