Swift program to check a string contains a specified suffix

Here, we are going to learn how to check a string contains a specified suffix in Swift programming language?
Submitted by Nidhi, on June 11, 2021

Problem Solution:

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.

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.

Swift String Programs »





Comments and Discussions!

Load comments ↻





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