replaceFirstChar

Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
@JvmName("replaceFirstCharWithChar") inline fun String.replaceFirstChar(
    transform: (Char) -> Char
): String
@JvmName("replaceFirstCharWithCharSequence") inline fun String.replaceFirstChar(
    transform: (Char) -> CharSequence
): String

Returns a copy of this string having its first character replaced with the result of the specified transform, or the original string if it's empty.

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
println("kotlin".replaceFirstChar { it.uppercase() }) // Kotlin

val sentence = "Welcome to Kotlin!"
val words = sentence.split(' ');
println(words.joinToString(separator = "_") { word -> word.replaceFirstChar { it.lowercase() } }) // welcome_to_kotlin!
//sampleEnd
}

Parameters

transform - function that takes the first character and returns the result of the transform applied to the character.

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/replace-first-char.html