To check if a string is empty in Kotlin, you can use the isEmpty()
method of the String class.
In this example,
str1
with a value of an empty string ""
.isEmpty()
method on str1
to check if it is empty.isEmpty()
method returns true
if the string is empty, otherwise false
.fun main() {
val str1 = "";
val isEmpty = str1.isEmpty()
println("Is string empty: $isEmpty")
}
Is string empty: true
In this example,
str2
with a value of "Hello World"
.isEmpty()
method on str2
to check if it is empty.isEmpty()
method returns true
if the string is empty, otherwise false
.fun main() {
val str2 = "Hello World";
val isEmpty = str2.isEmpty()
println("Is string empty: $isEmpty")
}
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in Kotlin language with well detailed examples.