How to check if an string is empty in Swift
In this tutorial, we are going to learn about how to check if the string is empty or not in Swift.
Checking string is empty
To check if a given string is empty or not, we can use the built-in isEmpty
property in Swift.
The isEmpty
property returns true if an string is empty; otherwise returns false.
Here is an example:
var place = ""
print(place.isEmpty)
Output:
true
Similarly, we can also check it using the count
property in Swift.
var place = ""
if(place.count == 0){
print("string is empty")
}else{
print("string is not empty")
}
Output:
"string is empty"