Skip to content

Commit 14f5181

Browse files
committed
String split tests
1 parent 1c24e0a commit 14f5181

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.string;
2+
3+
import org.apache.commons.lang.StringUtils;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
public class SplitTest {
11+
12+
@Test
13+
public void givenString_whenSplit_thenRetrunsArray_through_JavaLangString() {
14+
assertArrayEquals("split by comma", Arrays.asList("peter", "james", "thomas").toArray(), "peter,james,thomas".split(","));
15+
assertArrayEquals("split by whitespace", Arrays.asList("car", "jeep", "scooter").toArray(), "car jeep scooter".split(" "));
16+
assertArrayEquals("split by hyphen", Arrays.asList("1", "120", "232323").toArray(), "1-120-232323".split("-"));
17+
assertArrayEquals("split by dot", Arrays.asList("192", "168", "1", "178").toArray(), "192.168.1.178".split("\\."));
18+
assertArrayEquals("split by a regex", Arrays.asList("b", "a", "e", "l", "d", "u", "n", "g").toArray(),
19+
"b a, e, l.d u, n g".split("\\s+|,\\s*|\\.\\s*"));
20+
}
21+
22+
@Test
23+
public void givenString_whenSplit_thenRetrunsArray_through_StringUtils() {
24+
StringUtils.split("car jeep scooter");
25+
26+
assertArrayEquals("split by whitespace", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car jeep scooter"));
27+
assertArrayEquals("split by space, extra spaces ignored", Arrays.asList("car", "jeep", "scooter").toArray(),
28+
StringUtils.split("car jeep scooter"));
29+
assertArrayEquals("split by colon", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car:jeep:scooter", ":"));
30+
assertArrayEquals("split by dot", Arrays.asList("car", "jeep", "scooter").toArray(), StringUtils.split("car.jeep.scooter", "."));
31+
}
32+
}

0 commit comments

Comments
 (0)