String original = "This is a sentence.Rajesh want to test the application for the word split.";
List matchList = new ArrayList();
Pattern regex = Pattern.compile(".{1,10}(?:\\s|$)", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(original);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
System.out.println("Match List "+matchList);
I need to parse text into an array of strings no longer than 10 characters long and should not be interrupted by a word at the end of a string.
I used the logic below in my script, but the problem is analyzing the nearest space after 10 characters if there is a gap at the end of the line
for example: Actual sentence: “ This is a sentence. Rajesh wants to test the application for separating words. ” But after executing the logic, getting it, as shown below.
List of matches [This, nce.Rajesh, want, test, apply, for a word, split.]
source
share