- 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
Difference between isNullOrEmpty and isNullOrBlank in Kotlin
Both these functions, isNullOrEmpty and isNullOrBlank, are used in Kotlin whenever it is required to check if a String value is empty or not. Let's check how these two functions are different from each other.
isNullOrBlank – It takes whitespaces into account, which means " " is different from "". This function will return True only when the String is declared with no characters in it. It will check whether the value of the String is NULL and it will also check whether the String is blank.
isNullOrEmpty() – This function checks whether the string is declared as NULL or whether it is empty. Whenever String.length returns "0", then this function will return True, otherwise False.
Example – isNullOrBlank vs. isNullOrEmpty
The following example demonstrates the difference between isNullorBlank and isNullOrEmpty −
fun main(args: Array<String>) { val String1 = " " val String2 = "" // String1 is Null but not empty // String1 contains whitespace println(String1.isNullOrEmpty()) // Null and empty: True // isNullOrBlank counts whitespace as blank println(String1.isNullOrBlank()) // String2 has no whitespaces println(String2.isNullOrEmpty()) println(String2.isNullOrBlank()) }
Output
It will produce the following output −
false true true true
- Related Articles
- Difference between "fold" and "reduce" in Kotlin
- Difference between "*" and "Any" in Kotlin generics
- Difference between List and Array types in Kotlin
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- Difference between Java and Kotlin in Android with Examples
- Difference between a "class" and an "object" in Kotlin
- What's the difference between "!!" and "?" in Kotlin?
- IsNullOrEmpty() Method in C#
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between "var" and "val" in Kotlin?
- Difference Between & and &&
- How to communicate between Activity and Service in Android using Kotlin?
- Difference between and in Ionic framework?
- Difference between == and === operator in JavaScript
- Difference between "." and "#" selector in CSS
