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)?
The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token. For example, if you pass single space “ ” as a delimiter to this method and try to split a String.
Using toArray() MethodThe toArray() function of the List class can also be used to convert a string to array in Java. It takes a list of type String as the input and converts each entity into a string array.
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]", ""); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With