- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - isNotEmpty() Function
The Kotlin string isNotEmpty() function is used to check whether the string has a character sequence. If the string contains character sequence returns true; Otherwise returns false.
This function is commonly used in scenarios where we want to validate input, handle optional data, or avoid operations on empty strings.
Syntax
Following is the syntax of the Kotlin string isNotEmpty() function −
fun CharSequence.isNotEmpty(): Boolean
Parameters
This function does not accepts any parameters.
Return value
This function returns a Boolean value, which is true if the string is not empty; Otherwise false.
Example 1: Check If a String Empty or Not
Following is the basic example, here how we can use isNotEmpty() function with a string in kotlin −
fun main() {
// non empty string
val string = "tutorialspoint"
val res = string.isNotEmpty()
if(res == true){
println("string is not empty")
}
else {
println("string is empty")
}
}
Output
Following is the output −
string is not empty
Example 2: Input Validation
Now, let's see another example. For input validation, we are using the isNotEmpty() function to check if a user has provided input in a form field before proceeding −
fun main() {
val userInput = "Aman123"
if (userInput.isNotEmpty()) {
println("Your Input is not empty but it is wrong.")
} else {
println("Input field is empty")
}
}
Output
If input field is filled with wrong details −
Your Input is not empty but it is wrong.
Example 3: If the String is Empty
In the following example, the isNotEmpty function returns false, if the string is empty −
fun main() {
val userInput = ""
val res = userInput.isNotEmpty()
println("is string not empty?: ${res}")
}
Output
Following is the output −
is string not empty?: false