🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
isNotBlankFunction Syntax- Examples
- Basic Usage
- Using
isNotBlankin Conditional Statements - Comparing
isNotBlankwithisNotEmpty
- Real-World Use Case
- 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:
trueif the string is not blank,falseotherwise.
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment