improve split string using pattern (#11945)

This commit is contained in:
Azhwani 2022-03-20 15:19:29 +01:00 committed by GitHub
parent c6e2b905f5
commit 2e4b518c4b
1 changed files with 7 additions and 11 deletions

View File

@ -3,8 +3,9 @@ package com.baeldung.split;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
@ -29,16 +30,11 @@ public class SplitStringEveryNthChar {
}
public static List<String> usingPattern(String text, int n) {
List<String> results = new ArrayList<>();
Pattern pattern = Pattern.compile(".{1," + n + "}");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String match = text.substring(matcher.start(), matcher.end());
results.add(match);
}
return results;
return Pattern.compile(".{1," + n + "}")
.matcher(text)
.results()
.map(MatchResult::group)
.collect(Collectors.toList());
}
public static List<String> usingGuava(String text, int n) {