BAEL-4878 | How to split a string, but also keep the delimiters?

This commit is contained in:
root 2021-05-06 18:38:50 +00:00
parent 7c344b57c3
commit 656d3a9ad7
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package com.baeldung.splitkeepdelimiters;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import com.google.common.base.Splitter;
public class SplitAndKeepDelimitersUnitTest {
private final String positivelookAheadRegex = "((?=@))";
private final String positivelookBehindRegex = "((?<=@))";
private final String positivelookAroundRegex = "((?=@)|(?<=@))";
private final String positiveLookAroundMultiDelimiterRegex = "((?<=;|:|,|#|@|~)|(?=;|:|,|#|@|~))";
private String text = "Hello@World@This@Is@A@Java@Program";
private String textMixed = "@Hello;World@This:Is,A#Java~Program";
private String textMixed2 = "pg-no.10@hello;world@this:is,a#10words|Java~Program";
@Test
public void givenString_splitAndKeepDelimiters_using_javaLangString() {
assertThat(text.split(positivelookAheadRegex)).containsExactly("Hello", "@World", "@This", "@Is", "@A", "@Java", "@Program");
assertThat(text.split(positivelookBehindRegex)).containsExactly("Hello@", "World@", "This@", "Is@", "A@", "Java@", "Program");
assertThat(text.split(positivelookAroundRegex)).containsExactly("Hello", "@", "World", "@", "This", "@", "Is", "@", "A", "@", "Java", "@", "Program");
assertThat(textMixed.split(positiveLookAroundMultiDelimiterRegex)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
}
@Test
public void givenString_splitAndKeepDelimiters_using_ApacheCommonsLang3StringUtils() {
assertThat(StringUtils.splitByCharacterType(textMixed2)).containsExactly("pg", "-", "no", ".", "10", "@", "hello", ";", "world", "@", "this", ":", "is", ",", "a", "#", "10", "words", "|", "J", "ava", "~", "P", "rogram");
}
@Test
public void givenString_splitAndKeepDelimiters_using_GuavaSplitter() {
assertThat(Splitter.onPattern(positiveLookAroundMultiDelimiterRegex)
.splitToList(textMixed)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
assertThat(Splitter.on(Pattern.compile(positiveLookAroundMultiDelimiterRegex))
.splitToList(textMixed)).containsExactly("@", "Hello", ";", "World", "@", "This", ":", "Is", ",", "A", "#", "Java", "~", "Program");
}
}