Kotlin String isNotBlank

The isNotBlank function in Kotlin is used to check if a string is not blank. A string is considered not blank if it contains one or more non-whitespace characters. This function is part of the Kotlin standard library and provides a straightforward way to determine if a string contains meaningful characters.

Table of Contents

  1. Introduction
  2. isNotBlank Function Syntax
  3. Examples
    • Basic Usage
    • Using isNotBlank in Conditional Statements
    • Comparing isNotBlank with isNotEmpty
  4. Real-World Use Case
  5. Conclusion

Introduction

The isNotBlank function checks whether a string is not blank, meaning it contains one or more non-whitespace characters. This function is useful for validating input, checking conditions, and handling strings in various applications.

The isNotBlank function returns true if the string contains one or more non-whitespace characters. Otherwise, it returns false.

isNotBlank Function Syntax

The syntax for the isNotBlank function is as follows:

fun CharSequence.isNotBlank(): Boolean

Parameters:

  • This function does not take any parameters.

Returns:

  • true if the string is not blank, false otherwise.

Examples

Basic Usage

To demonstrate the basic usage of isNotBlank, we will create a few strings and check if they are not blank.

Example

fun main() {
    val emptyString = ""
    val whitespaceString = "   "
    val nonBlankString = "Kotlin"

    println("Is the emptyString not blank? ${emptyString.isNotBlank()}")
    println("Is the whitespaceString not blank? ${whitespaceString.isNotBlank()}")
    println("Is the nonBlankString not blank? ${nonBlankString.isNotBlank()}")
}

Output:

Is the emptyString not blank? false
Is the whitespaceString not blank? false
Is the nonBlankString not blank? true

Using isNotBlank in Conditional Statements

This example shows how to use isNotBlank in conditional statements to perform actions based on whether a string is not blank.

Example

fun main() {
    val userInput = "Hello, World!"

    if (userInput.isNotBlank()) {
        println("User input: $userInput")
    } else {
        println("User input is blank. Please enter some text.")
    }
}

Output:

User input: Hello, World!

Comparing isNotBlank with isNotEmpty

This example demonstrates the difference between isNotBlank and isNotEmpty. The isNotEmpty function checks if a string has one or more characters, while isNotBlank also ensures that the string contains non-whitespace characters.

Example

fun main() {
    val emptyString = ""
    val whitespaceString = "   "
    val nonBlankString = "Kotlin"

    println("Is emptyString not blank? ${emptyString.isNotBlank()}")
    println("Is emptyString not empty? ${emptyString.isNotEmpty()}")

    println("Is whitespaceString not blank? ${whitespaceString.isNotBlank()}")
    println("Is whitespaceString not empty? ${whitespaceString.isNotEmpty()}")

    println("Is nonBlankString not blank? ${nonBlankString.isNotBlank()}")
    println("Is nonBlankString not empty? ${nonBlankString.isNotEmpty()}")
}

Output:

Is emptyString not blank? false
Is emptyString not empty? false
Is whitespaceString not blank? false
Is whitespaceString not empty? true
Is nonBlankString not blank? true
Is nonBlankString not empty? true

Real-World Use Case

Validating Form Input

In real-world applications, the isNotBlank function can be used to validate form input, such as ensuring that required fields are filled with meaningful content.

Example

fun main() {
    val username = "user123"
    val password = "   "

    if (username.isNotBlank() && password.isNotBlank()) {
        println("Form submitted successfully.")
    } else {
        if (username.isBlank()) {
            println("Username cannot be blank.")
        }
        if (password.isBlank()) {
            println("Password cannot be blank.")
        }
    }
}

Output:

Password cannot be blank.

Conclusion

The isNotBlank function in Kotlin is a convenient method for checking if a string is not blank. It provides a simple way to validate and handle strings that contain meaningful characters. 

By understanding and using this function, you can effectively manage string validation and condition checking in your Kotlin applications.

Comments