|
| 1 | +package com.baeldung.multipledelimiterssplit; |
| 2 | + |
| 3 | +import com.google.common.base.CharMatcher; |
| 4 | +import com.google.common.base.Splitter; |
| 5 | +import com.google.common.collect.Iterators; |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.TestInstance; |
| 10 | + |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 15 | +public class MultipleDelimitersSplitUnitTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void givenString_whenSplittingByMultipleDelimitersWithRegEx_thenStringSplit() { |
| 19 | + String example = "Mary;Thomas:Jane-Kate"; |
| 20 | + String[] names = example.split(";|:|-"); |
| 21 | + String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"}; |
| 22 | + Assertions.assertEquals(4, names.length); |
| 23 | + Assertions.assertArrayEquals(expectedNames, names); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenString_whenSplittingByWithCharMatcherAndOnMethod_thenStringSplit() { |
| 28 | + String example = "Mary;Thomas:Jane-Kate"; |
| 29 | + String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"}; |
| 30 | + Iterable<String> expected = Arrays.asList(expectedArray); |
| 31 | + Iterable<String> names = Splitter.on(CharMatcher.anyOf(";:-")).split(example); |
| 32 | + Assertions.assertEquals(4, Iterators.size(names.iterator())); |
| 33 | + Assertions.assertIterableEquals(expected, names); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenString_whenSplittingByWithRegexAndOnPatternMethod_thenStringSplit() { |
| 38 | + String example = "Mary;Thomas:Jane-Kate"; |
| 39 | + String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"}; |
| 40 | + Iterable<String> expected = Arrays.asList(expectedArray); |
| 41 | + Iterable<String> names = Splitter.on(Pattern.compile(";|:|-")).split(example); |
| 42 | + Assertions.assertEquals(4, Iterators.size(names.iterator())); |
| 43 | + Assertions.assertIterableEquals(expected, names); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenString_whenSplittingByMultipleDelimitersWithGuava_thenStringSplit() { |
| 48 | + String example = "Mary;Thomas:Jane-Kate"; |
| 49 | + String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"}; |
| 50 | + Iterable<String> expected = Arrays.asList(expectedArray); |
| 51 | + Iterable<String> names = Splitter.onPattern(";|:|-").split(example); |
| 52 | + Assertions.assertEquals(4, Iterators.size(names.iterator())); |
| 53 | + Assertions.assertIterableEquals(expected, names); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenString_whenSplittingByMultipleDelimitersWithApache_thenStringSplit() { |
| 58 | + String example = "Mary;Thomas:Jane-Kate"; |
| 59 | + String[] expectedNames = new String[]{"Mary", "Thomas", "Jane", "Kate"}; |
| 60 | + String[] names = StringUtils.split(example, ";:-"); |
| 61 | + Assertions.assertEquals(4, names.length); |
| 62 | + Assertions.assertArrayEquals(expectedNames, names); |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
0 commit comments