Kotlin String - uppercase() Function



The Kotlin string uppercase() function converts all characters of this string to uppercase and return a copy of this string 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 uppercase forms, such as numbers or punctuation marks, they remain unchanged.

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

Syntax

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

fun String.uppercase(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 upper case of this string.

Example 1: Convert a String into UpperCase

Following is the basic example, we use the uppercase() to make the string upper case −

fun main() {
   val string = "tutorialspoint India"

   // use uppercase()
   val upperCase = string.uppercase()
   println("UpperCase of the string: " + upperCase)
}

Output

Following is the output −

UpperCase of the string: TUTORIALSPOINT INDIA

Example 2: Use the UpperCase() with Specific Locale

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

import java.util.Locale
fun main(){
   val input = "hello, tutorialspoint!"
   val uppercaseString = input.uppercase(Locale.US)
   println(uppercaseString)
}

Output

Following is the output −

HELLO, TUTORIALSPOINT!
kotlin_strings.htm
Advertisements