Is there is more succinct way to get the prefix of...
# getting-started
a
Is there is more succinct way to get the prefix of a string if and only if it ends with a suffix? Kotlin Playground
Copy code
val names = listOf(
    "commonMain",
    "commonTest",
    "commonTestFixtures",
)

println(names.joinToString { getPrefix(it) })
// expected: common, common, common

fun getPrefix(name: String) =
    when {
        name.endsWith("Main") -> name.substringBeforeLast("Main")
        name.endsWith("Test") -> name.substringBeforeLast("Test")
        name.endsWith("TestFixtures") -> name.substringBeforeLast("TestFixtures")
        else -> error("invalid suffix")
    }
y
Playground:
Copy code
val names = listOf(
    "commonMain",
    "commonTest",
    "commonTestFixtures",
    "jvmMain",
    "jvmTest",
    "jvmTestFixtures",
    "jsMain",
    "jsTest",
    "jsTestFixtures",
)

val suffixes = listOf("Main", "Test", "TestFixtures")

fun getPrefix(name: String) = 
    suffixes.find(name::endsWith)?.let(name::substringBeforeLast) ?: error("invalid suffix")


fun main() {
    println(names.joinToString { getPrefix(it) })
}
👍 1
j
There is
removeSuffix
which does exactly what you ask for, but chaining calls for different suffixes only works if you can guarantee that no item will have several suffixes in a row. E.g.
commonTestMain
would yield
common
, which might not be desirable.
n
another (more complicated but potentially more efficient) solution:
Copy code
val names = listOf(
    "commonMain",
    "commonTest",
    "commonTestFixtures",
    "jvmMain",
    "jvmTest",
    "jvmTestFixtures",
    "jsMain",
    "jsTest",
    "jsTestFixtures",
)

val suffixes = listOf("Main", "Test", "TestFixtures").joinToString("|", prefix="(.+)(", postfix=")") { """\Q$it\E""" }.toRegex()

fun getPrefix(name: String) = suffixes.matchEntire(name)?.groupValues?.get(1) ?: error("invalid suffix")

fun main() {
    println(names.joinToString { getPrefix(it) })
}
y
I don't think that solution would be more efficient for a small list of suffixes, due to the overhead of compiling regex. I think for like thousands of suffixes though sure it might perform better.
n
Agreed
I like the earlier approach better. Regex starts to shine if the condition is not as simple as suffix/prefix