Q:

Swift program to check a string contains a specified suffix

belongs to collection: Swift String Programs

0

Here, we will check a string contains a specified suffix or not using the hasSuffix() function. The hasSuffix() function returns true, if the string contains a specified suffix string otherwise it returns false.

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 contains a specified suffix is given below. The given program is compiled and executed successfully.

// Swift program to check a string contains
// a specified suffix

var str = "ABCDE"

if(str.hasSuffix("DE"))
{
    print("DE is a suffix of string str")
}
else
{
    print("DE is not a suffix of string str")
}
if(str.hasSuffix("CD"))
{
    print("CD is a suffix of string str")
}
else
{
    print("CD is not a suffix of string str")
}

Output:

DE is a suffix of string str
CD is not a suffix of string str

...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 a string str initialized with "ABCDE". Then we checked string str contains a specified suffix or not using the hasSuffix() function and printed the appropriate message 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 append a string using the additio... >>
<< Swift program to check a string contains a specifi...