Feature/bael 2118 dhawal (#5231)

* [BAEL-2118] - Add a section in String Split Article

* [BAEL-2118] - Formatting and upgraded to JUNIT 5
This commit is contained in:
Dhawal Kapil 2018-09-13 10:19:41 +05:30 committed by Predrag Maric
parent af074ee8d3
commit 107e6a75be
1 changed files with 16 additions and 1 deletions

View File

@ -2,10 +2,11 @@ package com.baeldung.string;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.Test; import org.junit.jupiter.api.Test;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
@ -54,4 +55,18 @@ public class SplitUnitTest {
assertThat(resultList) assertThat(resultList)
.containsExactly("car", "jeep", "scooter"); .containsExactly("car", "jeep", "scooter");
} }
@Test
public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_Regex() {
assertThat(" car , jeep, scooter ".trim()
.split("\\s*,\\s*")).containsExactly("car", "jeep", "scooter");
}
@Test
public void givenStringContainsSpaces_whenSplitAndTrim_thenReturnsArray_using_java_8() {
assertThat(Arrays.stream(" car , jeep, scooter ".split(","))
.map(String::trim)
.toArray(String[]::new)).containsExactly("car", "jeep", "scooter");
}
} }