Kotlin String - lowercase() Function



The Kotlin string lowercase() function returns a copy of the current string that is converted to lowercase using the rules of the default locale or the specified locale. This function is especially used when working with text processing, user input validation, and formatting.

If the string contains characters that do not have lowercase forms, such as numbers or punctuation marks, they remain unchanged.

This function serves as a replacement for the toLowerCase() function, which has been deprecated.

Syntax

Following is the syntax of the Kotlin string lowercase() function −

fun String.lowercase(locale: Locale): String

Parameters

This function accepts an optional parameter which is a locale object representing the specific locale used for the conversion. If not provided it uses the system's default locale.

Return value

This function returns a string which is lower case of this string.

Example 1: Convert String into Lowercase

Following is the basic example, we use lowercase() to make the string lower case −

fun main() {
   val string = "TUTORIALSPOINT, India"

   // use lowercase()
   val lowerCase = string.lowercase()
   println("lowerCase of the string: " + lowerCase)
}

Output

Following is the output −

lowerCase of the string: tutorialspoint, india

Example 2: Use Specific Locale

In this example, we pass the specific locale to the lowercase() function to convert the string into lowercase −

import java.util.Locale
fun main(){
   val input = "HELLO, KOTLIN!"
   val lowercaseString = input.lowercase(Locale.UK)
   println(lowercaseString)
}

Output

Following is the output −

hello, kotlin!
kotlin_strings.htm
Advertisements