56

In Java, we've always been reminded to use myString.isEmpty() to check whether a String is empty. In Kotlin however, I find that you can use either myString == "" or myString.isEmpty() or even myString.isBlank().

Are there any guidelines/recommendations on this? Or is it simply "anything that rocks your boat"?

Thanks in advance for feeding my curiosity. :D

6 Answers 6

67

Don't use myString == "", in java this would be myString.equals("") which also isn't recommended.

isBlank is not the same as isEmpty and it really depends on your use-case.

isBlank checks that a char sequence has a 0 length or that all indices are white space. isEmpty only checks that the char sequence length is 0.

/**
 * Returns `true` if this string is empty or consists solely of whitespace characters.
 */
public fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }


/**
 * Returns `true` if this char sequence is empty (contains no characters).
 */
@kotlin.internal.InlineOnly
public inline fun CharSequence.isEmpty(): Boolean = length == 0
Sign up to request clarification or add additional context in comments.

1 Comment

Since Kotlin 1.3, there's even a new extension function available which is ifBlank and it allows you to assign a default string if your main value is empty
43

For String? (nullable String) datatype, I use .isNullOrBlank()

For String, I use .isBlank()

Why? Because most of the time, I do not want to allow Strings with whitespace (and .isBlank() checks whitespace as well as empty String). If you don't care about whitespace, use .isNullorEmpty() and .isEmpty() for String? and String, respectively.

3 Comments

I've also thought of always using isBlank(), but did you know that passwords could actually be just spaces? Of course, that depends on what you'd allow. Thanks for the reminder on nullable String! Here's an invisible upvote. :D
Depends on the situation, but I would assume that an only blank space password would be disallowed by most websites/backends. Then again, I've never tried it! But +1 for extreme edge cases
String can be null, so i always choose ~string.isNullOrBlank()
12

Use isEmpty when you want to test that a String is exactly equal to the empty string "".

Use isBlank when you want to test that a String is empty or only consists of whitespace ("", " ").

Avoid using == "".

Comments

9

There are two methods available in Kotlin.

  1. isNullOrBlank()
  2. isNullOrEmpty()

And the difference is:

data = " " // this is a text with blank space 
 
println(data.isNullOrBlank()?.toString())  //true
println(data.isNullOrEmpty()?.toString())  //false

Comments

4

You can use isNullOrBlank() to check is a string is null or empty. This method considers spaces only strings to be empty.

Here is a usage example:

val s: String? = null
println(s.isNullOrBlank())
val s1: String? = ""
println(s1.isNullOrBlank())
val s2: String? = "     "
println(s2.isNullOrBlank())
val s3: String? = "  a "
println(s3.isNullOrBlank())

The output of this snippet is:

true
true
true
false

Comments

2

As someone mentioned in the comments, you can use ifBlank, like so:

fun getSomeValue(): String {
  // ...
  val foo = someCall()
  return foo.ifBlank { "some-default" }
}

Documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/if-blank.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.