replace First Char
inline fun String.replaceFirstChar(transform: (Char) -> CharSequence): String
Content copied to clipboard
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.
Since Kotlin
1.5
Samples
import samples.*
import java.util.Locale
import kotlin.test.*
fun main() {
//sampleStart
assertPrints("kotlin".replaceFirstChar { it.uppercase() }, "Kotlin")
val sentence = "Welcome to Kotlin!"
val words = sentence.split(' ');
assertPrints(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.