Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 678
AnimatedRNG Avatar asked Jan 12 '11 22:01

AnimatedRNG


People also ask

How do you split a string sentence into an array of 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.

Can we convert string to array in Java?

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.


1 Answers

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]", ""); } 
like image 79
Adam Batkin Avatar answered Sep 18 '22 17:09

Adam Batkin