Skip to content

Commit be57468

Browse files
authored
[rm-first-char] remove the first char from a string (#663)
1 parent 2d50d7f commit be57468

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.removeTheFirstChar
2+
3+
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.api.assertThrows
5+
import kotlin.test.assertEquals
6+
7+
class RemoveTheFirstCharUnitTest {
8+
9+
@Test
10+
fun `when calling drop() then get the expected result`() {
11+
val myStr = "0a b c d e"
12+
val result = myStr.drop(1)
13+
assertEquals("a b c d e", result)
14+
15+
//works when the string is empty
16+
val result2 = "".drop(1)
17+
assertEquals("", result2)
18+
}
19+
20+
@Test
21+
fun `when calling subString() then get the expected result`() {
22+
val myStr = "0a b c d e"
23+
val result = myStr.substring(1)
24+
assertEquals("a b c d e", result)
25+
26+
//throw exception when the string is empty
27+
assertThrows<IndexOutOfBoundsException> {
28+
"".substring(1)
29+
}
30+
}
31+
32+
@Test
33+
fun `when calling takeLast() then get the expected result`() {
34+
val myStr = "0a b c d e"
35+
val result = myStr.takeLast(myStr.length - 1) // or takeLast(myStr.lastIndex)
36+
assertEquals("a b c d e", result)
37+
38+
//works when the string is empty
39+
val myEmpty = ""
40+
41+
assertThrows<IllegalArgumentException> {
42+
myEmpty.takeLast(myEmpty.length - 1)
43+
}.also {
44+
assertEquals("Requested character count -1 is less than zero.", it.message)
45+
}
46+
47+
}
48+
49+
@Test
50+
fun `when using replace() then get the expected result`() {
51+
val myStr = "0a b c d e"
52+
val result = myStr.replace("^.".toRegex(), "")
53+
assertEquals("a b c d e", result)
54+
55+
//works when the string is empty
56+
val result2 = "".replace("^.".toRegex(), "")
57+
assertEquals("", result2)
58+
}
59+
}

0 commit comments

Comments
 (0)