Swift – Check if String is Empty

To check if string is empty in Swift, use the String property String.isEmpty.

isEmpty property is a boolean value that is True if there are no any character in the String, or False if there is at least one character in the String.

Example

In the following program, we will take an empty string, and check programmatically if the string is empty using String.isEmpty property.

main.swift

var str = ""
var result = str.isEmpty
print( "Is the string empty? \(result)" )

Output

Is the string empty? true

Example 2 – Non-empty String

In the following program, we will take a string with "Hello World" value, and check programmatically if the string is not empty using String.isEmpty property.

main.swift

var str = "Hello World"
var result = str.isEmpty
print( "Is the string empty? \(result)" )

Output

Is the string empty? false

Conclusion

In this Swift Tutorial, we learned how to check if a String is empty or not using String.isEmpty property.