Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
To use the `toUpperCase()` function, we simply call it on a string variable or string literal. Here’s an example:
val str = "hello, world!"
val upperCaseStr = str.toUpperCase()
println(upperCaseStr) // Output: HELLO, WORLD!
In the above example, we first declare a string variable `str` with the value “hello, world!”. We then call the `toUpperCase()` function on `str` and assign the result to a new variable `upperCaseStr`. Finally, we print the value of `upperCaseStr` to the console, which outputs “HELLO, WORLD!”.
It’s important to note that the `toUpperCase()` function returns a new string with all the characters converted to uppercase. The original string remains unchanged.
In addition to the `toUpperCase()` function, the Kotlin standard library also provides a `toLowerCase()` function to convert a string to lowercase letters.
Overall, using the `toUpperCase()` function in Kotlin is a simple and straightforward way to convert a string to uppercase letters.