Skip to content

Commit 94bc7b5

Browse files
authored
[improve-str-split] add split by spaces (#783)
1 parent c3b8182 commit 94bc7b5

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

core-kotlin-modules/core-kotlin-strings/src/test/kotlin/com/baeldung/split/StringSplitUnitTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ class StringSplitUnitTest {
5757
assertThat(info.split(pattern, 3)).containsExactly("28", "32", "2 / 64 = 29")
5858
}
5959

60+
@Test
61+
fun `when split by regex, should use regex object instead of string regex`() {
62+
val info = "a b c d"
63+
64+
//split by literal regex string won't work in Kotlin:
65+
assertThat(info.split("\\s+")).containsExactly(info)
66+
67+
// a Regex object is required:
68+
assertThat(info.split(Regex("\\s+"))).containsExactly("a", "b", "c", "d")
69+
assertThat(info.split("\\s+".toRegex())).containsExactly("a", "b", "c", "d")
70+
}
71+
6072
@Test
6173
fun `splitToSequence works lazily`() {
6274
val info = "random_text,".repeat(1000)
@@ -89,4 +101,4 @@ class StringSplitUnitTest {
89101
assertEquals("grapes", fruitsArray[2])
90102
assertEquals("orange", fruitsArray[3])
91103
}
92-
}
104+
}

0 commit comments

Comments
 (0)