How could I separate a String using a pre-given List of Strings, separating them by spaces?
Eg:
List of words: words = {"hello", "how", "are", "you"}
The string I want to separate: text = "hellohowareyou"
public static String separateText(String text, List<String> words) {
String new_text;
for (String word : words) {
if (text.startsWith(word)) {
String suffix = text.substring(word.length()); //'suffix' is the 'text' without it's first word
new_text += " " + word; //add the first word of the 'string'
separateString(suffix, words);
}
}
return new_text;
}
And new_text should return hello how are you
Note that the order of the List words could be different and also have more words, like a dictionary.
How could I make this recursion, if needed?