Converting a sentence string to a string array of words in Java
I need my Java program to take a string like:
"This is a sample sentence."
and turn it into a string array like:
{"this","is","a","sample","sentence"}
No periods, or punctuation (preferably). By the way, the string input is always one sentence.
Is there an easy way to do this that I'm not seeing? Or do we really have to search for spaces a lot and create new strings from the areas between the spaces (which are words)?
String.split() will do most of what you want. You may then need to loop over the words to pull out any punctuation.
For example:
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]", "");
}
Now, this can be accomplished just with split
as it takes regex:
String s = "This is a sample sentence with []s.";
String[] words = s.split("\\W+");
this will give words as: {"this","is","a","sample","sentence", "s"}
The \\W+
will match all non-alphabetic characters occurring one or more times. So there is no need to replace. You can check other patterns also.