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)?
Try using the following:
That will create a substring at each index of the array of strings using the space as a split point.
The easiest and best answer I can think of is to use the following method defined on the java string -
And just do "This is a sample sentence".split(" "). Because it takes a regex, you can do more complicated splits as well, which can include removing unwanted punctuation and other such characters.