I need my Java program to take a string like:
\"This is a sample sentence.\"
and turn it into a string array like:
{\"this\
Try using the following:
String str = "This is a simple sentence";
String[] strgs = str.split(" ");
That will create a substring at each index of the array of strings using the space as a split point.
Another way to do that is StringTokenizer. ex:-
public static void main(String[] args) {
String str = "This is a sample string";
StringTokenizer st = new StringTokenizer(str," ");
String starr[]=new String[st.countTokens()];
while (st.hasMoreElements()) {
starr[i++]=st.nextElement();
}
}
You can use simple following code
String str= "This is a sample sentence.";
String[] words = str.split("[[ ]*|[//.]]");
for(int i=0;i<words.length;i++)
System.out.print(words[i]+" ");
You can also use BreakIterator.getWordInstance
.