Swift Program to check a string end with a specified substring


Swift provides an hasSuffix() function to check a string end with a specified substring. The hasSuffix() function returns a boolean value which indicates whether the specified substring matches the ending characters of the input string. The hasSuffix() function is case sensitive that means according to this function “t”, and “T” are two different values.

Input

String = “Today is cloudy day”
SubString = “day”

Output

yes

Here, the character of the substring matches the ending characters of the input string.

Syntax

func hasSuffix(value)

Here, the value represents a string. This function returns true if the value matches the ending characters of the input string. Otherwise, it will return false. If also return false if the value is an empty string.

Algorithm

  • Step 1 − Create a string.

  • Step 2 − Create a substring.

  • Step 3 − Use hasSuffix() function to determine whether the given string ends with the substring or not.

  • Step 4 − Print the output.

Example 1

In the following Swift program, we will check a string that ends with a specified substring. So create a string and two substrings. Then use the hasSuffix() function to determine whether the input string ends with the substring or not. If the input string ends with the substring, then the hasSuffix() function will return true. Else, it will return false.

import Foundation
import Glibc

let myStr = "Car color is green" 
let suffixStr1 = "green" 
let suffixStr2 = "blue"

// Checking whether the string ends with the 
// suffix substring or not
var res1 = myStr.hasSuffix(suffixStr1)
var res2 = myStr.hasSuffix(suffixStr2)

print("Is '\(myStr)'ends with the substring'\(suffixStr1)'?:", res1)
print("Is '\(myStr)'ends with the substring'\(suffixStr2)'?:", res2)

Output

Is 'Car color is green'ends with the substring'green'?: true
Is 'Car color is green'ends with the substring'blue'?: false

Example 2

In the following Swift program, we will check a string that ends with a specified substring. So create a string and a substring. Then use the hasSuffix() function to determine whether the input string ends with the substring or not. If the input string ends with the substring, then print "YES! the given string ends with the specified substring.”. Otherwise, print “ NO! the given string does not end with the specified substring."

import Foundation
import Glibc

let myStr = "Learn Swift programming" 
let suffixStr = "programming" 

// Checking if the string ends with the 
// suffix substring or not
if myStr.hasSuffix(suffixStr) {
   print("YES! the given string ends with the specified substring.")
} else {
   print("NO! the given string does not end with the specified substring.")
}

Output

YES! the given string ends with the specified substring.

Conclusion

So this is how we can check whether a string ends with a specified substring or not using the hasSuffix() function. This method compares every character of the substring with the ending character of the input or given string. If any of the characters do not match the given string, then this function will return false.

Updated on: 09-May-2023

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements