how to make a sentence into an array in java code example

Example 1: turning a sentence to an array java

String s = "This is a sample sentence.";
String[] words = s.split("\\s+");
for (int i = 0; i < words.length; i++) {
    // You may want to check for a non-word character before blindly
    // performing a replacement
    // It may also be necessary to adjust the character class
    words[i] = words[i].replaceAll("[^\\w]", "");
}

Example 2: how to convert a sentence into string array in java

String str = "This is a simple sentence";
String[] strgs = str.split(" ");