Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin : what is difference between isNullOrEmpty and isNullOrBlank?

Tags:

kotlin

I want check value of my EditText in android so I saw two function for my String value :

user.isNullOrBlank()

and

user.isNullOrEmpty()

what is difference between them?

like image 274
mohosyny Avatar asked Apr 01 '20 20:04

mohosyny


People also ask

Is null or blank VS is null or empty?

empty refers to an empty string (a string with no characters, whose length is zero), whereas blank refers to a string that is either empty or consists entirely of whitespace characters.

How do you pass empty string in Kotlin?

To create an empty String in Kotlin, assign the variable with empty double quotes "" , or with String class constructor String() .


1 Answers

isNullOrBlank() takes whitespace into account:

fun main() {   val thisIsBlank = "   "    println(thisIsBlank.isNullOrEmpty())   println(thisIsBlank.isNullOrBlank()) } 

This prints:

false true 

because thisIsBlank is not empty, but it is blank.

like image 55
CommonsWare Avatar answered Sep 27 '22 21:09

CommonsWare