Q:

Swift program to check a string is empty or not

belongs to collection: Swift String Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 str1str2str3. Then we check each string is empty or not using the isEmpty() function and print appropriate messages on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift program to concatenate two strings... >>
<< Swift program to create a multi-line string litera...