This Kotlin example shows how to check a string is empty/blank in Kotlin.
Kotlin distinguishes between empty and blank strings. An empty string does not have any characters, a blank string contains any number of white spaces.
Kotlin Empty/Blank String Example
The example tests if a string is blank and empty.
package net.javaguides.kotlin.examples
fun main(args: Array < String > ) {
val s = "\t"
if (s.isEmpty()) {
println("The string is empty")
} else {
println("The string is not empty")
}
if (s.isBlank()) {
println("The string is blank")
} else {
println("The string is not blank")
}
}
Output:
The string is not empty
The string is blank
The isEmpty() returns true if the string is empty:
if (s.isEmpty()) {
The isBlank() returns true if the string is blank:
if (s.isBlank()) {
Comments
Post a Comment