substringBefore

Returns a substring before the first occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

fun String.substringBefore(delimiter: Char, missingDelimiterValue: String = this): String(source)
fun main() {
    val text1 = "Hello,World"
    val beforeComma = text1.substringBefore(',')
    println(beforeComma) // prints "Hello"

    val text2 = "Hello"
    val beforeCommaWithDefault = text2.substringBefore(',', "N/A")
    println(beforeCommaWithDefault) // prints "N/A"
}

Source