To check if a string is empty in Swift, you can use the isEmpty
property of the String class.
In this example,
str1
with an empty string value ""
.isEmpty
property on str1
to check if it is empty.isEmpty
property returns true
if the string is empty, otherwise false
.let str1 = ""
let isEmpty = str1.isEmpty
print("Is string empty: \(isEmpty)")
Is string empty: true
In this example,
str2
with a non-empty string value "Hello World"
.isEmpty
property on str2
to check if it is empty.isEmpty
property returns true
if the string is empty, otherwise false
.let str2 = "Hello World"
let isEmpty = str2.isEmpty
print("Is string empty: \(isEmpty)")
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in Swift language with well detailed examples.