Coiiin :
I have three lists, that I have merged into one list
static List allLists = Stream.of(list1, list2, list3)
.flatMap(Collection::stream)
.collect(Collectors.toList());
I have user input without whitespaces
String = "HelloIwanttobookanonlineseminaratyourcompany"
All the words in the user input String
are already in allLists
. I want to iterare over allLists
and insert whitespaces into the String, with every word found. The result should be:
String = "Hello I want to book an online seminar at your company"
Is there an easy solution I miss?
Bohemian :
Use a one-liner that employs a (massive) look behind built from allLists
to insert spaces before each word:
str = str.replaceAll("(?<=" + String.join("|", allLists) + ")", " ");
Note that order of words in allLists
is important; if you want longer words to take preference, list them first (recommended). Eg if both "book" and "booking" are in your list, put booking before book, otherwise you’ll get "book ing" in your result.