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)?
You can use
BreakIterator.getWordInstance
to find all words in a string.Test:
Ouput:
Following is a code snippet which splits a sentense to word and give its count too.
Another way to do that is StringTokenizer. ex:-
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:
Now, this can be accomplished just with
split
as it takes regex: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.You can use simple following code