Kotlin String - isBlank() Function



The Kotlin string isBlank() function is used to check whether a string is empty or contains only whitespace character such as spaces, tabs, or line breaks. This function is particularly useful for validating user input or cleaning up data.

Returns true if the string is empty or contains solely whitespace characters. Otherwise, return false if the string contains at least one non-whitespace character.

Syntax

Following is the syntax of the Kotlin string isBlank() function −

fun isBlank(): Boolean

Parameters

This function does not accepts any parameters.

Return value

This function returns a Boolean value, true if the string is empty or contains solely whitespace character; Otherwise false.

Example 1

Following is the basic example, here how we can use isBlank() with an empty string in Kotlin −

fun main() {
   val str = ""
   println(str.isBlank())
}

Output

Following is the output −

true

Example 2

Now, let's look at another example of what the isBlank() function returns if the string contains whitespace.

fun main() {
   val str = "   "
   println(str.isBlank())
}

Output

Following is the output −

true

Example 3

let's see what the isBlank() function returns if the string contains non_whitespace character. −

fun main() {
  val str = "tutorialspoint"
  println(str.isBlank())
}

Output

Following is the output −

false
kotlin_strings.htm
Advertisements