BAEL-5195 split string by multiple delimiters (#11356)
* BAEL-5195 split string by multiple delimiters * Delete baeldun * added assertions of the content Co-authored-by: Matea Pejčinović <matea.pejcinovic@intellexi.hr>
This commit is contained in:
parent
1ab0a19d25
commit
d17c2be712
|
@ -52,6 +52,11 @@
|
|||
<artifactId>semver4j</artifactId>
|
||||
<version>${semver4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -80,6 +85,7 @@
|
|||
<assertj.version>3.6.1</assertj.version>
|
||||
<spring-core.version>5.3.9</spring-core.version>
|
||||
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
|
||||
<guava.version>31.0.1-jre</guava.version>
|
||||
<maven-artifact.version>3.6.3</maven-artifact.version>
|
||||
<gradle-core.version>6.1.1</gradle-core.version>
|
||||
<jackson-core.version>2.11.1</jackson-core.version>
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.multipledelimiterssplit;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterators;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class MultipleDelimitersSplitUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithRegEx_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] names = example.split(";|:|-");
|
||||
String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Assertions.assertEquals(4, names.length);
|
||||
Assertions.assertArrayEquals(expectedNames, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByWithCharMatcherAndOnMethod_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.on(CharMatcher.anyOf(";:-")).split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByWithRegexAndOnPatternMethod_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.on(Pattern.compile(";|:|-")).split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithGuava_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
Iterable<String> expected = Arrays.asList(expectedArray);
|
||||
Iterable<String> names = Splitter.onPattern(";|:|-").split(example);
|
||||
Assertions.assertEquals(4, Iterators.size(names.iterator()));
|
||||
Assertions.assertIterableEquals(expected, names);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenSplittingByMultipleDelimitersWithApache_thenStringSplit() {
|
||||
String example = "Mary;Thomas:Jane-Kate";
|
||||
String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"};
|
||||
String[] names = StringUtils.split(example, ";:-");
|
||||
Assertions.assertEquals(4, names.length);
|
||||
Assertions.assertArrayEquals(expectedNames, names);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue