Swift String isEmpty Property



String isEmpty Property

The isEmpty property is a pre-defined property of the String structure in Swift. The isEmpty property is used to check whether the given string is empty or not. It will return a boolean value to represent whether the string is empty or not.

For example, we have a string Tutorials Point, now the isEmpty property will return false means the given string is not empty.

Syntax

Following is the syntax of the isEmpty Property −

string.isEmpty

Return Value

This property returns true if the given string is empty. Or return false if the given string is not empty.

Example 1

Swift program to demonstrate the isEmpty property −

import Foundation

// Declaring a string
var str = "Mohit likes Swift and C++"

// Checking whether the string is empty or not
// Using isEmpty property
print("Is str is empty?:", str.isEmpty)

Output

Is str is empty?: false

Example 2

Swift program to determine whether the given string is empty or not using the isEmpty property −

import Foundation

// Declaring a string
var str = ""

// Checking whether the string is empty or not
// Using isEmpty property
print("Is str is empty?:", str.isEmpty)

Output

Is str is empty?: true

Example 3

Swift program to check whether the given string is empty or not −

import Foundation

// Empty string
var str = ""

// Using isEmpty property
if str.isEmpty{
   print("String is empty")
}else{
   print("String has some values")
}

Output

String is empty
swift_strings.htm
Advertisements