How do i check string is lower case or uppercase in Kotlin

Published October 06, 2021

In this kotlin example tutorial we will learn how to check given string is lower case or upper case and how do i convert string to lower case and upper case.

To check the given string contains lower case or upper case letter we will use below two methods

  • isLowerCase()
  • isUpperCase()

 

Now let's check the Given Sting contains lower or upper case

  • Take Sting
  • Let's iterate String for each character
  • Then use the methods isLowerCase(), isUpperCase() methods

Based on the return values we can say it contains lower or upper case characters

fun main()
{

   val caseString = "This is String Case examples "
    for (c in caseString)
    {
        
        if (c.isLowerCase())
        {
            print("Contains Lower case")
            break;
        }
        if (c.isUpperCase())
        {
            print("Contains Upper case")
            break;
        }
    
    }

}

 

How do we convert convert string to upper case?

To convert given string to upper case we will use toUpperCase() method.

fun main()
{

   var caseString = "This is String Case examples "

caseString=caseString.toUpperCase();
print(caseString)

}

 

Convert String to Lower case

To convert given string to upper case we will use toLowerCase() method.

fun main()
{

   var caseString = "This is String Case examples "

caseString=caseString.toLowerCase();
print(caseString)

}

 

Output:

this is string case examples 

 

Conclusion: In this kotlin example we learned how to check String contains lower case/upper case letters and how to convert given string to lower/upper case.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2726 Views