fun String.uppercase(): String {
val array = toCharArray()
array.forEachIndexed { index, c ->
if (c in 'a'..'z') {
array[index] = Char(c.code - 32)
}
}
return String(array)
}
Alternatively, we can also use the previously defined Char#uppercaseChar()
function :
fun String.uppercase(): String {
val array = toCharArray()
array.forEachIndexed { index, c ->
array[index] = c.uppercaseChar()
}
return String(array)
}