- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a string is empty in Kotlin?
In this article, we will see three different ways to check whether a String is empty in Kotlin.
Example – isEmpty()
Kotlin library function isEmpty() can be used in order to check whether a String is empty. Note that it counts spaces too.
fun main(args: Array<String>) { // No space between the quotes val myString = "" // Space between the quotes val anotherString = " " println(myString.isEmpty()) // this will return false as we have a space println(anotherString.isEmpty()) }
Output
On execution, it will produce the following output −
true false
Observe that the second string is not empty because it contains a space.
Example – isBlank()
isBlank() can be used to check whether a String value is blank or not. We will be getting True for both the scenario used above, as the value actually contains a blank value.
fun main(args: Array<String>) { // Without Space val myString = "" // With Space val anotherString = " " // Both the strings are blank println(myString.isBlank()) println(anotherString.isBlank()) }
Output
On execution, it will produce the following output −
true true
Example – isNullOrBlank() or isNullOrEmpty()
isNullOrBlank() and isNullOrEmpty() are the other two functions that can be used to check whether a given string is empty or not.
fun main(args: Array<String>) { // string without space val myString = "" // string with a space val anotherString = " " //true :: null and blank println(myString.isNullOrBlank()?.toString()) //true ::null and empty println(myString.isNullOrEmpty()?.toString()) //true :: null and blank println(anotherString.isNullOrBlank()?.toString()) //false :: null but not empty; contains whiteSpace println(anotherString.isNullOrEmpty()?.toString()) }
Output
It will generate the following output −
true true true false
- Related Articles
- How to Check if Android EditText is empty in Kotlin?
- How to check if String is empty in Java?
- Check if a String is empty ("") or null in Java
- Java Program to check if a string is empty or not
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- Check if a String is whitespace, empty ("") or null in Java
- How to check if a list is empty in Python?
- How to check if a C# list is empty?
- How to check if a Laravel collection is empty?
- Check if a String is not empty ("") and not null in Java
- What is the correct way to check if String is empty in Java?
- How to check if PSCustomObject is empty in PowerShell?
- Python program to check if the string is empty or not
- How to check if android editText is empty?

Advertisements