Swift program to check a string is empty or not

Here, we are going to learn how to check a string is empty or not in Swift programming language?
Submitted by Nidhi, on June 10, 2021

Problem Solution:

Here, we will create string variables and check strings are empty or not using the isEmpty() function, and print the appropriate message on the console screen.

Program/Source Code:

The source code to check a string is empty or not is given below. The given program is compiled and executed successfully.

// Swift program to check a string 
// is empty or not

var str1 = ""
var str2 = String()
var str3 = "Hello World"

if(str1.isEmpty)
{
    print("str1 is an empty string")
}
else
{
    print("str1 is not empty string")
}
if(str2.isEmpty)
{
    print("str2 is an empty string")
}
else
{
    print("str2 is not empty string")
}
if(str3.isEmpty)
{
    print("str3 is an empty string")
}
else
{
    print("str3 is not empty string")
}

Output:

str1 is an empty string
str2 is an empty string
str3 is not empty string

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created three string variables str1, str2, str3. Then we check each string is empty or not using the isEmpty() function and print appropriate messages on the console screen.

Swift String Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.