forked from igorwojda/kotlin-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
29 lines (23 loc) · 821 Bytes
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.igorwojda.string.decapitalizeconst
// Kotlin idiomatic solution
private object Solution1 {
private fun decapitalizeConst(str: String): String {
val subsStringsList = str.split("_").map { it.toLowerCase().capitalize() }
return subsStringsList.joinToString("").decapitalize()
}
}
// Another Approach
private object Solution2 {
private fun decapitalizeConst(str: String): String? {
val words = str.split("_").filter { it.isNotEmpty() }
if (words.size <= 1) return null
return words.mapIndexed { index, word ->
if (index == 0) {
word.toLowerCase()
} else {
word.first().toUpperCase() + word.drop(1).toLowerCase()
}
}.joinToString(separator = "")
}
}
private object KtLintWillNotComplain