I need to split a string in every three words (using JAVA). For example:
"This is an example of what I need."
The output would be:
This is an
is an example
an example of
example of what
of what I
what I need
I need
Thanks for the help!
I've tried like this. I don´t know how to get the last two words or what to do when the phrase has less than 3 words.
String phrase = "This is an example of what I need."
String[] splited = phrase.split(" ");
for (int i = 0; i < phraseSplited.length - 2; i++) {
threeWords = splited[i] + " " + splited[i+1] + " " + splited[i+2];
System.out.println(threeWords);
}