Get lowercase and uppercase representation of a string in Kotlin
This article explores different ways to get lowercase and uppercase representation of a string in Kotlin.
The recommended solution to convert all characters in the string to uppercase is using the toUpperCase() function. A typical invocation for this method would look like:
|
1 2 3 4 |
fun main() { val s = "abc" println(s.toUpperCase()) // ABC } |
To convert all characters in the string to lowercase, use toLowerCase() function:
|
1 2 3 4 |
fun main() { val s = "ABC" println(s.toLowerCase()) // abc } |
Both toUpperCase() and toLowerCase() function accepts a Locale. It changes the case of the string using the rules of the specified locale. Its usage is demonstrated below.
|
1 2 3 4 5 6 7 8 |
import java.util.* fun main() { val s = "Abc" println(s.toLowerCase(Locale.ENGLISH)) // abc println(s.toUpperCase(Locale.ENGLISH)) // ABC } |
That’s all about getting lowercase and uppercase representation of a string in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)