To convert a string to uppercase in Kotlin, you can use the toUpperCase()
method of the String class.
In this example,
str1
with a string value of "Hello World"
.toUpperCase()
method of the string object str1
to convert it to uppercase and store the result in upperCaseStr1
variable.toUpperCase()
method returns a new string with all characters converted to uppercase.upperCaseStr1
to standard output.fun main() {
val str1 = "Hello World";
val upperCaseStr1 = str1.toUpperCase();
println("Uppercase of given string is: $upperCaseStr1");
}
Uppercase of given string is: HELLO WORLD
In this example,
str2
with a string value of "apple"
.toUpperCase()
method of the string object str2
to convert it to uppercase and store the result in upperCaseStr2
variable.toUpperCase()
method returns a new string with all characters converted to uppercase.upperCaseStr2
to standard output.fun main() {
val str2 = "apple";
val upperCaseStr2 = str2.toUpperCase();
println("Uppercase of given string is: $upperCaseStr2");
}
Uppercase of given string is: APPLE
In this tutorial, we learned How to convert String to Uppercase in Kotlin language with well detailed examples.